Best way to pass JSON from Browser to PHP using Ajax.Request - php

Hi I have a JSON object that is a 2-dimentional array and I need to pass it to PHP using Ajax.Request (only way I know how). ...Right now I manually serialized my array using a js function...and get the data in this format: s[]=1&d[]=3&[]=4 etc. ....
my question is: Is there a way to pass the JSON object more directly/efficientely?..instead of serializing it myself?
Thanks for any suggestions,
Andrew

You can also use Prototype's function toJSON() to convert an array into a JSON object. After passing it to server via Ajax call, simply use PHP's function json_decode() to decode the object.

Pass the object as a JSON-string to PHP, and in PHP use the builtin json_decode to get a PHP-object from the string.
In Javascript, use a "stringify" function on your object to get it as a string, library available for example here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

In que Javascript side (with Prototye):
var myJSON= Object.toJSON(youArray);
In que Php side:
$myjson = $_POST['myjson'];
$arrayJSON= json_decode(stripslashes($myjson), true);

Check
http://www.openjs.com/scripts/data/ued_url_encoded_data/
to encode nested data directly correct, since Object.toQueryString() doesn't accept nested data...

Related

PHP object notation generator similar to json

I have some data structure in PHP that I need to dump into a format that can be natively parsed as PHP code. Is there a tool for that?
In other words, is there a PHP data structure formatter that would output PHP object notation code, a sort of "PSON" for PHP similar to what JSON is for JavaScript?
Use build in function var_export.
There is, it is called serialize: http://php.net/manual/en/function.serialize.php

Data stored in json

I have my data stored in a JSON string like these...
a:1:{s:15:"s2member_level1";s:1:"1";}
How can i read this values in mysql?
I need to know if the value "s2member_level1" is 1.
Thanks!!!
That's not JSON but a string resulted from calling serialize() in PHP. You cannot parse it easily in MySQL. If you can use PHP, use the unserialize function:
$obj = unserialize($data_from_mysql);
if ($obj['s2member_level1'] == 1) {
// more code here
}
You can convert data to JSON in PHP using the json_encode function. In a similar way, you construct an object from a JSON string using json_decode.
#Lekensteyn is correct, but you could do a like statement, although its performance would most likely be very poor. My true answer is to change how you store this information to take advantages of best performing queries.
select * from table
where column like '%s:15:"s2member_level1";s:1:"1";%';
#Lekensteyn is right about the type of this particular String, but for others, PHP has json_decode which you can use to convert a JSON object to a PHP object. It would be considerably more difficult to read such an object using MySQL only.
This is no json, but serialized data. It was probably serialized with the 'serialize' function of PHP. Try:
print_r(unserialize('a:1:{s:15:"s2member_level1";s:1:"1";}'));
... to unserialize it.

How to return multiple items with AJAX and PHP

Is it possible to return multiple items via AJAX and PHP? I am reading up on it and it seems just like an echo in PHP and responseText via Javascript. Can I return an array of items via PHP and convert it into Javascript or HTML?
It depends on the data format your PHP response is sending - it sounds like the examples you're seeing are just plain text. The best format in my opinion is JSON which can send an array of multiple results, and even a multi-dimensional array many levels deep.
See json_encode() in PHP and JSON examples on jquery.com.
You can use the JSON format to return multiple values in AJAX: http://www.php.net/manual/en/function.json-encode.php
You can then decode it in javascript. This function allows you to do it in jQuery: http://api.jquery.com/jQuery.getJSON/

reading json encoded php array using ajax request

I have a php array with key, value pairs. I have json encoded it. And using ajax.Request i called the php file in which that array is there. Now i have to access both key and value pairs. Could anyone let me know how to do that?
You need to parse the JSON.
You can use the JSON library.
You can use a library method like jQuery's $.parseJSON().
If the JSON is trusted, you can use eval().
Code in javascript "json.js" is required. you can download this.
var votedCommentString = getCookie("votedComment"); // get value of voted comment cookie
votedComment = JSON.parse(votedCommentString); // extract json object into array
votedComment[votedComment.length] = id; // add more data in array
var cookieData = JSON.stringify(votedComment); // convert array into json object
setCookie("votedComment", cookieData, 365); // and again set cookie
In PHP you can access this in following way
$cookieData = (array)json_decode($_COOKIE["votedComment"]); // extract json object into array
print_r($cookieData);
Use
json_encode($array_variable) // to convert array in to json
As you said the array is in php file which is called via ajax, you can simply decode the json encoded string and retrieve the array with keys and values respectively.
Simply use json_decode() function to retrieve the array.

pass multidimentional array from javascript to php

Im running a javascript code which reads values from different XML files and it generates a multidimentional array based on those values. Now I need to pass this array to a PHP page. I tried different but it always pass the arrray as string not as an array.
Anyone has an idea :( ... and thank you very much
What Caleb said.
Use this and JSON encode your JS array to a string, send it over to PHP and use json_decode to decode it into a PHP array.
You need a JSON encoder/decoder to do that. Prototype has it implemented by default and with jQuery you can use jQuery-JSON
For example if you use Prototype as your JS library then you can convert your array into a string like that:
var example_multi_dim_arr = {"a":[1,2,3], "b": [4,5,6]};
var string_to_be_sent_to_server = Object.toJSON(example_multi_dim_arr);
And in the PHP side (assuming that the JSON string is passed to the script as a POST variable)
$multi_dim_arr = json_decode($_POST["variable_with_json"], true);
The last true field in json_decode indicates that the output should be in the form of an array ($multi_dim_arr["a"]) and not as an object ($multi_dim_arr->a).
NB! the function json_decode is not natively available in PHP 4, you should find a corresponding JSON class if you are using older versions of PHP. In PHP 5 everything should work fine.

Categories