I have an array, myData=[[1,2,3],[4,5,6],....,[..,..,..]]
I want to post this to the server.
Usually, for a JS variable, I just put the variable into a textbox and then submit the form using JS. However, when I put the 2D array into a textbox, JS converts it to a string, such that it becomes a 1D array, which looks like:
[1,2,3,4,5,6....]
I want to be able to post the entire 2D array to the server and retrieve it on the next page using PHP. How do I do that?
Thanks!
You should use JSON to turn the array into a string with javascript and then parse the string back into an array in php.
for example:
var myData = var data = [['hooray',1],['test','meow'],[0,3,2]];
var myData_string = JSON.stringify(myData);
This will turn your object/array into a string which you can then POST and parse with php like so:
$myData = json_decode($input);
References:
JSON (javascript)
json_decode() (php)
Related
I am getting this type of object from PHP. {"3":4} Starting array is another value which is encoded into json and echoed. And the rest is another array which is encoded into json and echoed.
How to process this in react native.
{"3":4}{"1":{"section":"8CSB","lecture_id":"24","subject_name":"MySQL","subject_code":"8CS2","date":"2020-04-29","lecture_no":"1"},"2":{"section":"8CSB","lecture_id":"25","subject_name":"Android","subject_code":"8CS3","date":"2020-04-29","lecture_no":"2"},"3":{"section":"8CSB","lecture_id":"26","subject_name":"Mobile Computing","subject_code":"8CS1","date":"2020-04-29","lecture_no":"3"},"4":{"section":"8CSB","lecture_id":"43","subject_name":"UNPS Lab","subject_code":"8CS5","date":"2020-04-29","lecture_no":"5"},"5":{"section":"8CSB","lecture_id":"45","subject_name":"UNPS Lab","subject_code":"8CS5","date":"2020-04-29","lecture_no":"6"}}
It's JavaScript at the end of the day and that's JSON - so you just parse it in JS.
var Object = JSON.parse(jsonstring);
For a more detailed explanation of what to do, try this which will go into the picker.
As for the first part, it's malformed, by the looks of things, so you cannot parse it.
To remove the first part, if it is consistently this, you can do it like so:
var Inputobject = <your input>;
var jsonstring = Inputobject.slice(7);
var Object = JSON.parse(jsonstring);
I'm creating a simple d3 chart which is linked to data from a MySQL server (with PHP handling those queries). I've formatted the data in PHP to be JSON compliant, and finally used the PHP function json_encode to pass the data to javascript. Below, I've included the important lines of code:
1) The packer array is created in JSON format and is d3-friendly too.
$packer[] = array('key' => $subject, 'values' => $array);
//where $subject is a string and $array is (you guessed it) an array of data
2) In the d3 chart constructor, data is initialized as an array, in JSON format
var data = <?=json_encode($packer)?>;
//here, I echo the php array to javascript in JSON format
When I load the page, the chart appears, but no data is loaded. I therefore know this problem lies in the loading of the data. I'd appreciate any help!
EDIT: I found the answer! It was a problem with my JSON data using arrays but d3 required an object formatted slightly differently. Link for those curious:
https://stackoverflow.com/questions/19500742/plotting-json-data-using-nvd3
$temp['values'][$i]['x']= $i;
$temp['values'][$i]['y']= intval($row['temperature']);
I have a problem with the json functionality in zend and js.
I try to encode a single array containing some number of models like this:
echo json_encode(Application_Model_Marker::getMarkers());
var mark = JSON.parse(jsonVal); //in js
where getMarkers is a static method that returns an array of marker models.
This works fine and when I parse it in the js script and try accessing the values of the json object it works fine.
If however I try to create and send an array of array like this:
$allData = array();
$allData['info'] = Application_Model_Marker::getMarkers();
$allData['openingHours'] = Application_Model_Openinghours::getOpeningHours();
$allData['happyHours'] = Application_Model_Happyhour::getHappyHours();
echo json_encode($allData);
It still sends all the correct information when I try to alert(jsonVal.responseText); in js.
It has three arrays each containg some arrays of objects.
However when I try to initialize a variable to the parsed json object like in the first example, I can't access the values and it seems some kind of error occurs as the program stops when I try it.
I don't quite get it as it has all the correct info when i just try to print the response text from the encoded json object.
Any ideas how to do this multidimensional json encoding?
Try this, Hopefully it'll work :
<sctript>
var mark;
eval("mark = "+jsonVal+";");
</sctrip>
I am developing an application having more than thousands of values. I am trying to make a dynamic array in JavaScript. I'm using AJAX to get my values. So I have to create a string from PHP ,it should able to convert from string to array in JavaScript.
How can I make a string in PHP that can be converted to array in JavaScript?
You are looking for JSON and json_encode():
$string = json_encode($array);
The content of the string will be the array written in valid javascript.
Use JSON notation to create a string of values and then read it from JS. The simplest way to do this is something like that:
<script type="text/javascript">
var myPHPData = <?php echo json_encode($myData); ?>;
alert myPHPData; // now you have access to it in JS
</script>
if you dont intend to use json , you can do something of this kind..
you can create a string of this kind in php (separate it by any delimiter)
$arr = "1;2;3;4;5;6;7" ;
in javascript you can convert this to array using split function
//make an ajax call and get this string (say str)
arr = str.split(";");
split() returns an array
arr[0] is 1
arr[1] is 2
and so on !!
I am sending a CSV list to the server within the url. It is a list of songid's:
var $array = [];
oTable.find('td').each(function(){
$songdata = $(this).data('data');
$songid = $songdata.songid;
//The next line is the variable I need to also send along with each songid
$duration = $songdata.duration;
$array.push($songid)
});
This results in '24,25,32,48,87' being sent to the server.
I then use the following in my php script to convert it into an array:
$songs = $_GET['songid'];
$songarray = explode(",", $songs);
This works great, I can access the data properly and all is well.
However I now have the need to add another property to each of the songid's. As seen above, I need to add the $duration variable to the data sent and be able to convert it into an array server side. (or alternatively construct the actual array client side would be fine also)
How would I go about this?
You could use this to encode your array into JSON:
http://www.openjs.com/scripts/data/json_encode.php
And then send that String to your backend where you can unpack it with this:
$yourarray = json_decode($string)
I would create an object of the data in your jscript and send it over to the PHP and then use json_decode then you have an associative array of your data within the PHP
You always can use JSON for data serialization/deserealization as of Serializing to JSON in jQuery
try sending arrays to PHP in JSON format and then do json_decode($var) in your PHP script.
make an associative array server side
$array = new Array();
//add songid as key and duration as value
$array = array_push_assoc($array, $songid, $songduration);
echo json_encode($array);
on the client side after parsing the json
$.each(json,function(key,value){
//key will be the songid
//value will be the duration
});