Reputation: 13610
I would like to send a couple of arrays to my php with jquery and json, how do i do that?
Im working with:
$.ajax(
{
url: "script.php",
type: "POST",
data: ???,
dataType: "html",
success: function(tablehtml){
alert(tablehtml);
}
});
Typical i want to send along two arrays, and the php site would then produce a html table.
For instance: i want information about the users ["Tim", "Alan", "Kate"]
for the months ["June", "July", "August"]
I expect to json_decode in my php script, but how to i pack and send the two arrays? :D
Upvotes: 3
Views: 114
Reputation: 344517
Assuming your arrays are as simple as that, it wouldn't be very difficult to construct a valid JSON string without the use of another library:
var json = '{"users":["'+users.join('","')+'"],"months":["'+months.join('","')+'"]}';
$.ajax({
url: "script.php",
type: "POST",
data: json,
dataType: "html",
success: function(tablehtml) {
alert(tablehtml);
}
});
Upvotes: 1
Reputation: 342625
You don't necessarily need to decode json on the server side. Suppose you have:
var months = ["June", "July", "August"];
var names = ["Tim", "Alan", "Kate"];
Then you can use $.param to create a serialized representation of them, suitable for sending as a query string, like this:
$.ajax(
{
url: "script.php",
type: "POST",
data: $.param( { names: names, months: months } ),
dataType: "html",
success: function(tablehtml){
alert(tablehtml);
}
});
From PHP, what you'll see is two arrays with keys 'months' and 'names', so, for instance, you can loop over them like this:
foreach($_POST['names'] as $name) {
echo 'Name is: ' . $name . '<br />';
}
Upvotes: 2
Reputation: 471
You should be able to stringify your javascript array (serialize a value) using the JSON.stringify (on the json website) and to send it like this :
data:"myData="+JSON.stringify(myVar),
You will retrieve this value in the $_POST['myData'] and to json_decode it.
Upvotes: 0
Reputation: 2336
send data like this:
$.ajax(
{
url: "script.php",
type: "POST",
data:usersArray+'-|-'+monthsArray,
dataType: "html",
success: function(tablehtml){
alert(tablehtml);
}
});
In this case usersArray and monthsArray are just the variable names for your already existing JSON arrays.
and then explode that string in php
$arr = explode('-|-',$_REQUEST['data'])
then
$userArray = json_decode($arr[0])
$monthsArray = json_decode($arr[1])
i have done this in my project and its running fine
Upvotes: 0
Reputation: 630349
You can use JSON.stringify()
, like this:
$.ajax({
url: "script.php",
type: "POST",
data: JSON.stringify({users: usersArray, months: monthsArray}),
dataType: "html",
success: function(tablehtml){
alert(tablehtml);
}
});
In this case usersArray
and monthsArray
are just the variable names for your already existing arrays. On the PHP side when you deserialize the response, you're just looking for the users
and months
properties.
For older browsers that don't support JSON
natively (IE <8), just include json2.js
and it'll add support...and the above will still work.
Upvotes: 1