3

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

1
  • 2
    Your name alone should lead you to the answer :) Commented Dec 15, 2010 at 12:35

5 Answers 5

2

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 />'; 
}
Sign up to request clarification or add additional context in comments.

5 Comments

I believe you don't need $.param. Just give the object straight to $.ajax and it will convert it for you.
@box9 - that's correct, if data is an object, that's exactly what gets called under the covers.
@Nick - ah. Hmm, I was under the impression that it needs to be called explicitly to get the brackety representation: a[]=2&a[]=3&a[]=4. And it seems the behaviour of $.param is different depending on which version of jQuery is being used.
that's what the traditional setting is for ;), e.g. traditional: true on the request, $.ajaxSettings.traditional = true; or $.param(obj, true);, but it's jQuery 1.4+ that allows native brackety goodness...in previous versions there's no option, it's all default.
@Nick - Nice! Thanks for the input. I guess these comments will clear any doubts anyone might have :) And +1 for 'native brackety goodness'! :)
1

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.

Comments

1

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

1 Comment

This wouldn't have the effect you're after, since the strings need to be quoted in the array too ;)
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.