开发者

Post array of multiple checkbox values

开发者 https://www.devze.com 2022-12-08 21:52 出处:网络
Why is only one value of the \"db\" checkbox values array being sent to the server side script? JQUERY:

Why is only one value of the "db" checkbox values array being sent to the server side script?

JQUERY:

$(".db").live("change", function() {
    $(this).add($(this).next("label")).add($(this).next().next("br")).remove().insertAfter(".db:last + label + br"); 
    var url = "myurl.php";
    var db = [];
    $.each($('.db:checked'), function() {
        db.push($(this).val()); 
    });
    if(db.length == 0) { 
        db = "none"; 
    }       
    $.post(url, {db: db}, function(response) {
        $("#dbdisplay").html(response); 
    });
    return true;
开发者_JAVA技巧});

HTML:

<input type="checkbox" name="db[]" class="db" value="track"/><label for="track">track</label></br>
<input type="checkbox" name="db[]" class="db" value="gps"/><label for="gps">gps</label></br>
<input type="checkbox" name="db[]" class="db" value="accounting"/><label for="accounting">accounting</label></br>

Edit: I ended up answering my own question, but does anyone have documentation (or an explanation) of why this is necessary? It was difficult for me to find the exact answer (thus the posthumous post).


I agree with @jjclarkson. Just to add, instead of pushing your ids to an array, you can use $.map:

$(".db").live("change", function() {
    $(this).add($(this).next("label")).add($(this).next().next("br")).remove().insertAfter(".db:last + label + br"); 
    var url = "myurl.php";

    var db = $('.db:checked').map(function(i,n) {
        return $(n).val();
    }).get(); //get converts it to an array

    if(db.length == 0) { 
        db = "none"; 
    }       
    $.post(url, {'db[]': db}, function(response) {
        $("#dbdisplay").html(response); 
    });
    return true;
});


You need to have the square brackets to specify an array [] on the submitted variable name.

{'db[]': db}

$(".db").live("change", function() {
    $(this).add($(this).next("label")).add($(this).next().next("br")).remove().insertAfter(".db:last + label + br"); 
    var url = "myurl.php";
    var db = [];
    $.each($('.db:checked'), function() {
        db.push($(this).val()); 
    });
    if(db.length == 0) { 
        db = "none"; 
    }       
    $.post(url, {'db[]': db}, function(response) {
        $("#dbdisplay").html(response); 
    });
    return true;
});


var checkeditems = $('input:checkbox[name="review[]"]:checked')
                       .map(function() { return $(this).val() })
                       .get()
                       .join(",");
$.ajax({
    type: "POST",
    url: "/index.php/openItems/",   
    data: "ids=" + checkeditems,                                        
    success: function(msg) { $(".mainContainer").html(msg); }
});


$('input[name="mycheckboxes"]:checked').map(function(){ return $(this).val(); }).get().join(",");

then explode in PHP $mycheckboxes = explode(',',$_GET['mycheckboxes']);

0

精彩评论

暂无评论...
验证码 换一张
取 消