11

My overall goal is to get all some of all drop-downs on a page and send that to be processed by a php file.

Right now, the way I'm doing it in jQuery is making an overall schedule array and then adding each element to be updated to that array. So I have something like:

var schedule = [];
var data = { 
   'user_id' : '12', 
   'day_of_week' : 'Monday',
    'when' : 'start',
    'time' : '12 AM'
 }
schedule.push(data);
var data = { 
   'user_id' : '13', 
   'day_of_week' : 'Tuesday',
    'when' : 'end',
    'time' : '12 AM'
 }
schedule.push(data);
// schedule would have two objects in it

Obviously in loops and and stuff.

So, my schedule array has two objects in it, in this case.

Now, is it possible to use that schedule array as the ajax data? It doesn't work if I do something like:

$.ajax({
  url: 'http://something.com/ajax',
  data: schedule,
  type: 'POST'
});

But if I instead change it to schedule[0] it works just fine, but only for the first thing in the schedule array, obviously.

15
  • 1
    Remove the ' '..schedule is a variable, not a string. Also jQuery will encode your array as a JSON object you'll need to decode on the server. Commented Apr 21, 2012 at 16:53
  • 1
    Unless I'm mistaken, Schedule isn't an array of arrays. It is an array of objects, as data is an object. Commented Apr 21, 2012 at 17:01
  • 1
    @Truth: No specific reason besides the fact that I'm very new at ajax. Commented Apr 21, 2012 at 17:08
  • 3
    Interesting... what precise version of jQuery do you have there? Commented Apr 21, 2012 at 19:50
  • 2
    Oh good lord, I'm going to go cry in a corner. Some how the 7 in 1.7.2 got removed so it was just grabbing 1.2 from Google :/. Works now! Time to stop working on this for today. Commented Apr 21, 2012 at 19:54

3 Answers 3

11

The data attribute should be an object.

What you can do is this:

$.ajax({
  url: 'http://something.com/ajax',
  data: {schedule: schedule},
  type: 'POST'
});

So if you receive this for example in PHP you have $_POST["schedule"]. An that is exactly the same as you had in JavaScript.

Ohh yes I've forgot... also have a look at .serialize() and .serializeArray()!

Sign up to request clarification or add additional context in comments.

1 Comment

The current jQuery docs states it can be an object, string or an array.
3

Make sure you are using the correct version of jQuery. In earlier versions, you had to pass a sting; new versions use "intelligent guess" on the data variable. You can either explicitly tell jQuery that you're passing it a javascript object with the dataType parameter, or you can let jQuery figure it out.

Documentation

jQuery.ajax() - http://api.jquery.com/jQuery.ajax/

Comments

2

Pass it as JSON:

$.ajax({
  url: 'http://something.com/ajax',
  data: {schedule: schedule},
  type: 'POST',
  dataType: 'JSON'
});

It would send a JSON encoded string to the server, which server-side languages can parse. (in PHP, it's done with json_decode()).

4 Comments

What am I passing to json_deocde() though? $_POST['schedule'] ?
@Ben: Yes. schedule will consist of the the JSON string.
Either way, I keep getting nothing. I've tried doing type: 'POST' and type: 'JSON' with either combination of data and I keep getting a null value. What might be wrong? If I do a console.log(schedule) before the ajax part, I can see all my objects so I know they're there.
do you know the alternative of function json_decode() for python??

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.