简体   繁体   中英

Passing array to php through ajax

I have a list of different types of articles I display on my page. You can check or uncheck the types to make them display/hide from the article spawner. With my current array builder in jquery it cant be passed properly through ajax to the $_POST in php.

My function that is triggered everytime a checkbox is unchecked/checked

$(".jquery-checkbox").change(function(){
    var data = new Array();

   //check all the different types and see if they are checked or not
    $(".jquery-checkbox").each(function() {
        //if the type is not checked, put it into the data array to filter from results
        if(!$(this).is(':checked')){
            data[$(this).attr("name")] = $(this).attr("name");
        }
    });

    $.ajax({
            type: "POST",
            url: "/Ajax/article/articleList.php",
            dataType: "html",
            data: data,
            success: function(data) {$("#article-spawner").html(data);},
            error: function(){return false;}
    });
});

Is there a I can find each type that isn't checked and put them in an array in this form:

        var data = {
                "8":"not-checked",
                "10":"not-checked"
        };

It works with the above method but I'm not sure how to reach this method using .each

In JavaScript, arrays are used for sequential data with numeric indexes. While you can stick whatever properties you like on them, most tools which deal with them will ignore those properties.

This assumes that the name attributes of the inputs have, as is traditional, non-numeric names.

Use a regular object, not an array.

Change var data = new Array(); to var data = new Object(); or var data = {};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM