通过ajax传递数组

I'm fairly new to the jQuery execute on the same page stuff. So far I have been passing only single values trough ajax which is working fine. Now, I need to pass an array which is created by checkboxes through ajax.

My form html, dynamically created by php:

<input type=checkbox class=box name=box[] value=".$row['DoosID']." />

My jQuery:

    var BackorderDate   = $("#BackorderDate").val();
    var Box = $(".box").val();

        if( (TerugleverDatum == "") ){
            $("#backorderresult").html(" * Some Error .").fadeIn("Slow").fadeOut(3000);
        } else {
            $("#backorderresult").fadeOut();
            $.ajax({
                type    :'POST',
                url     :'./backorder.php',
                data    : box : Box,
                                              backorderdate: BackorderDate,
                dataType:"json",
                success: function(data){
                    // Do Something
             }
        });
    }

My PHP:

$boxlist = json_encode($box);

foreach($boxlist as $boxvalue){
   // Do something with every value
 }

this gives me a javascript error on submit saying box is not defined.

You might want to refer to the php file like this: url :'../backorder.php' (double dots). At least, that's how I do it always.

change this:

data    : box : Box, backorderdate: BackorderDate, // invalid way of sending

to this:

data    : {box : Box, backorderdate: BackorderDate}, // valid way

data

Type: PlainObject or String
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs.

More Info

Why don't you try enclosing all the data values in a open and close curly braze( { and } ) . Else it will conflict with the syntax of $.ajax method.

I think it is causing the problem.

You are sending data value in wrongly manner -

The data property should always be a JavaScript object. It's properties are serialized into a regular query string (for GET requests), or a normal post body parameter string (for POST requests). This serialized string is then sent to the server, along with the AJAX request. On the server you can read the properties of the data object as if they were sent as simple request parameters, via either GET or POST. Just like if the properties had been fields in a form.

Try this :

$.ajax({
    type    :'POST',
    url     :'./backorder.php',
    data    : 'box='+Box+'&backorderdate='+BackorderDate,
    dataType:"json",
    success: function(data){
        // Do Something
    }

});

For more information see jQuery Ajax