Get array from separate PHP file and store into jQuery array - javascript

I've searched around and wasn't able to find a clear path for accomplishing this.
I'm familiar with using json_encode() and json_decode() for converting php arrays to and from json, however I'm stumped about the below scenario:
I have a user interactive page where they need to access and use data from one of dozens of php array files. I have a php file (get-array.php) that determines which array to send back based on what the user has been selecting.
Normally I would just use .load() to load php results into a div, but I need the array back and stored in a jQuery array. What would be the best method for doing this?
Many thanks!

Use http://api.jquery.com/jQuery.ajax/ or the shorthand method, http://api.jquery.com/jQuery.get/ to retrieve JSON.
Or even better, this shorthand method is made just for this: http://api.jquery.com/jQuery.getJSON/
When you have your PHP array a d are ready to send it, you can use the JSON encode method of PHP to prepare it as JSON: http://php.net/manual/en/function.json-encode.php

Related

how can i get data from .js file and update this file

I don't know if this is the best way but I would like to have a .js file with an object that I will update once a day. I would not like to make a database for this because my code already works for the object. Via API I will get the data for the day and I would like to update the .js file. I would like to keep the historic data in this file and use it to feed the website, the API would only be used at the end of the day. It is a website with data from covid-19, I am doing it just for learning, so I am open to new approaches. I try to keep this file in github, but for edit this i need to put my user and pass in code, i dont know how turn around this issue.
Storing JavaScript objects to files is almost always done in JSON format.
Converting an object to a JSON string is done with the JSON.stringify() function.
When you read the JSON string back from the file, you covert it back to a JavaScript object with the JSON.parse() function.

json_encode(): Sending both a Table and Variables from PHP to JavaScript

From my PHP script I have to send to JavaScript two pieces of data in one AJAX return call:
relatively large array with app. 200 rows and 10 columns
set of variables providing additional information about that array
Both pieces of information should be returned from PHP to JavaScript in one JavaScript return call.
A standard way of sending arrays from PHP is by using json_encode(). This function however accepts only one parameter, either a string or an array.
How to, please, send both the table and variables from PHP to JavaScript then? If json_encode() wont handle this task, is there any other reliable way?
I have tried combining the variable set and the table into one array, and passing that array to JavaScript. This worked, however, when I wanted to get lenght of such array, where table and variables were combined, it simply did not work. Which is the reason, why I came to consult this problem here.
Thank you very much.
You can package the table array and the variable set array in to one single array:
$package = Array($dbarray, $vars);
$json_data = json_encode($package);

Passing a PHP array via Javascript to another PHP script

I may be going about this the hard way, as I modeled my code after an older article: populating triple dropdown list but I have found I don't need to query the database, because all the information is already neatly stored for me in a PHP array used earlier in the script.
I have a PHP array already created ($events):
<?php foreach($events as $event) {
Now I have to pass the events array to another file for processing:
<select name="search_month" onChange="getMonth('filter-leaderboard.php?month='+this.value)">
I'm just not sure how to pass it via Javascript.
How do I go about doing that?
Thanks!
Looking at your question, it appears you're trying to get the month using AJAX?
If so, it'd probably be better to post the array as the body rather than doing a get request.
You can use the json_encode function (http://au2.php.net/json_encode) to convert the array into a JSON object so you can work with it in JavaScript.

Traverse array list in Javascript file

In my Struts 2 based application, I have an ArrayList. How can I send this ArrayList to a Javascript file so that I can compare list item with element of jsp and show some message based on validation? How can I use the array list data in Javascript file.
I do not want use JSON.
JSON comes to mind - not sure of this example so here is one more
you can use <logic:iterate> and iterate through your List, here is example
Your question makes it seem like you want to send the ArrayList to the client directly, this means it will not be processed by a JSP. The easiest way to do this is with JSON, then XML, then perhaps some custom built format which you'd need to parse with JS.
But since you then say that you want to compare a value from the action class to a value in the JSP this means you'll want the comparison done when composing the view on the server and not on the client. For this see the struts2 tag documentation the <s:iterate> and <s:if> tags will let you do this.
As your question stands the two ideas are irreconcilable without some guess work.

JSON or XML or other data format with jQuery ajax()?

For a data send, where the return data contains potential updates for hundreds of elements on a page, is XML or JSON or another data format better, for usage with jQuery's various parsing formats (invoked via ajax() success)?
Check out this article, it outlines various pros/cons of XML, JSON and HTML when processing AJAX requests.
Personally I'd pick JSON as it uses less bandwidth & is easier to parse and use.
It sounds like a lot of data being returned so json. It's lighter and more compact. Plus it has native use instead of having to parse the xml and traverse it afterwards.
In javascript it is better to go with JSON because it is easier to code and less data to load from the server, unlike XML you have to write a code to parse the elements and fetch the values to your object and for every change in data tags or elements in XML you will need to modify your javascript code which means more coding and testing, unlike JSON all what you need is eval() and you are ready to go.

Categories

Resources