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
});
Related
I send a QueryString formatted text like bellow to a php Script via Ajax:
title=hello&custLength=200&custWidth=300
And I want to convert this text to a JSON Object by this result in PHP:
{
"title" : "hello",
"custLength" : 200,
"custWidth" : 300
}
How can i do that. Does anyone have a solution?
Edit :
In fact i have three element in a form by title , custLength and custWidth names and i tried to send these elements via serialize() jquery method as one parameter to PHP script.
this code is for Send data to php:
customizingOptions = $('#title,#custLength,#custWidth').serialize();
$.post('cardOperations',{action:'add','p_id':p_id,'quantity':quantity,'customizingOptions':customizingOptions},function(data){
if (data.success){
goBackBtn('show');
updateTopCard('new');
}
},'json');
in PHP script i used json_encode() for convert only customizingOptions parameter to a json.
But the result was not what I expected and result was a simple Text like this:
"title=hello&custLength=200&custWidth=300"
I realize this is old, but I've found the most concise and effective solution to be the following (assuming you can't just encode the $_GET global):
parse_str('title=hello&custLength=200&custWidth=300', $parsed);
echo json_encode($parsed);
Should work for any PHP version >= 5.2.0 (when json_encode() was introduced).
$check = "title=hello&custLength=200&custWidth=300";
$keywords = preg_split("/[\s,=,&]+/", $check);
$arr=array();
for($i=0;$i<sizeof($keywords);$i++)
{
$arr[$keywords[$i]] = $keywords[++$i];
}
$obj =(object)$arr;
echo json_encode($obj);
Try This code You Get Your Desired Result
The easiest way how to achiev JSON object from $_GET string is really simple:
json_encode($_GET)
this will produce the following json output:
{"title":"hello","custLength":"200","custWidth":"300"}
Or you can use some parse function as first (for example - save all variables into array) and then you can send the parsed output into json_encode() function.
Without specifying detailed requirements, there are many solutions.
I have a array of URL which I am saving in database after iteration on PHP side.
I am sending the array with Ajax and saving it with PHP.
Data send via Ajax
linksString=http://localhost/phpmyadmin/index.php?db=testdb&token=42d0dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=98604a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=9864dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=986042d0dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=986042d0dde57469a9a23&q=save
Not getting all values in $linksPieces , Getting only one value
But I am not getting this all string in PHP side; only getting first sub string which is before first comma(,).
I.E
http://localhost/phpmyadmin/index.php?db=testdb&token=42d0dde57469a9aa4b6a2f7e0741
PHP
$linksPieces = array();
$links = $_POST['linksString'];
$linksPieces = explode(",", $links);
foreach($linksPieces as $link)
{
//operation
}
I need to get all the string in array on PHP side.
If I am sending these type of url which have not any = then working fine.
http://in.yahoo.com/
http://www.hotmail.com/
http://www.google.com/
http://www.blah.com/
http://www.blah1.com/
You can try using encodeURIComponent() in Javascript before sending this string and using urldecode() in PHP before using this string.
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)
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 reading one document requests processed trough another document using AJAX.
In document processed trough AJAX i want to generate JSON array because its only way i can pass two variables and then spread them like this
$('#country').append($('<option>').text(arr_values[1]).attr('value', arr_values[0])));
now i generating code like this
$results2 = mysql_query('SELECT full, short FROM `Countries` WHERE '.$cities);
$json = array();
while( $result2 = mysql_fetch_array($results2) ) {
$json[] = $result2['short'].','.$result2['full'];
}
json_encode manual is pretty clear about its usage, check 'Example #2 A json_encode() example showing all the options in action' in http://ar.php.net/manual/en/function.json-encode.php
json_encode converts standard php arrays to JSON.
Anyway, your SQL code is not correct. You must use mysql_fetch_array for getting the values from results2, and 'short' and 'full' are not in your query.