php 发布多个复选框值的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1557273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:04:10  来源:igfitidea点击:

Post array of multiple checkbox values

phpjqueryhtmlformspost

提问by jjclarkson

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

为什么“db”复选框值数组中只有一个值被发送到服务器端脚本?

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

HTML:

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).

编辑:我最终回答了我自己的问题,但有没有人有文档(或解释)说明为什么这是必要的?我很难找到确切的答案(因此是死后的帖子)。

回答by karim79

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

我同意@jjclarkson。只是添加,而不是将您的 id 推送到数组,您可以使用$.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;
});

回答by Guest

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

回答by jjclarkson

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

回答by jjclarkson

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

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

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

然后在 PHP 中爆炸 $mycheckboxes = explode(',',$_GET['mycheckboxes']);