Passing a PHP array via Javascript to another PHP script - javascript

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.

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);

best way to relate a variable and its ID

I am getting results from a database with a simple php while loop, one of the pieces of information is a number that links to another table where the value is stored, I can think of plenty of ways to get this information linked and display the text related to the value but I want to know the fastest way to do it as I have a huge set of results so every bit of speed will make a difference. Is an array fastest, javascript? any advice you can give me would be great.
The schema would look something like this
col_table
colID(autonumber) colName(str) colState(int) colDate(date)
state_table
stateID(int) stateType(str)
I want to select the correct state type based on the colState matching a stateID and output the stateType while preserving the stateID for so I can edit the field and update the database using the number.
Using MySQL will be faster.
If you have to get through a PHP loop to read your results and make each time a new MySQL request, your script will take longer.
You can increase speed on MySQL by creating the right kind/amount of index, choosing wisely what is store in each field.
The later you parse content, the longer it will take. If you go for js, you will have to read a DB, loop trough it in PHP and do it again in JS, and making more request again ...
A join can be a good solution. A view can be even more easier to treat. Yuo can also consider caching results
Use a timer in php and try trial and error method. Use the time returned by the timer to evaluate speed and efficiency.
you should prepare your data on server side it is faster.
Whether you choose your server or database with a fast query it depends. If you have complex object graphs then the processing of results from db in order to create associations would be time consuming so an ORM is the way to go, otherwise as is your case with a simple join i would simply retrieve all data from db.
If you use php for rendering as well then render it using php no js.
If you use js for your ui then prepare data on server side and publish it via a REST webservice in json,i.e. usind json_encode functions of php, then retrieve it from js and output.

Get array from separate PHP file and store into jQuery array

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

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.

Categories

Resources