Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ajax之后在php中获取输入复选框值_Javascript_Jquery_Arrays_Ajax - Fatal编程技术网

Javascript 在ajax之后在php中获取输入复选框值

Javascript 在ajax之后在php中获取输入复选框值,javascript,jquery,arrays,ajax,Javascript,Jquery,Arrays,Ajax,我已经搜索了很多,但无法应用任何答案来解决我的问题 我有一个php生成的表单。形式结果如下所示: <form class="add-item"> <input type="checkbox" value="1" name="cust[]"> <input type="checkbox" value="2" name="cust[]"> <input type="checkbox" value="3" name="cust[]"> &

我已经搜索了很多,但无法应用任何答案来解决我的问题

我有一个php生成的表单。形式结果如下所示:

<form class="add-item">
  <input type="checkbox" value="1" name="cust[]">
  <input type="checkbox" value="2" name="cust[]">
  <input type="checkbox" value="3" name="cust[]">
  <input type="checkbox" value="4" name="cust[]">
  <button class="submit"> 
</form>
在我的PHP文件中,我只有以下内容:

$cust = $_POST['cust'];
print_r($cust);// to see what ajax send
在某种程度上,这段代码是有效的,但并不像我期望的那样

如果我只选中一个checkbok,我会得到以下结果:

cust[]='1' // the value of the checked checkbox
如果我选中多个1,我会得到一个数组,但是这个数组会忽略列表中的第一项。。。例如,下面的代码是如果我选中所有复选框。。。如您所见,第一个输入被忽略:

Array
(
    [0] => 2 // the value of the checked checkbox
    [1] => 3 // the value of the checked checkbox
    [2] => 4 // the value of the checked checkbox
)
我希望始终获得一个阵列(如果可能),因此,如果我这样做,客户将仅选择1个我获得:

Array
(
    [0] => 2 // the value of the checked checkbox
)
如果客户选择得更好,则数组中的所有值都将显示出来

有什么想法吗


p、 抱歉,如果我的代码词汇不是最好的

我认为更简单

 $(document).ready(function () {
            $('.add-item').submit(function (event) {
                var data = $('.add-item').serialize(); // send all the form data
                console.log(data); // for debug only
                $.ajax({
                    type: 'post',
                    url: 'form_ajax.php',
                    data: data,
                }).done(function (data) {
                    console.log(data); // response from server via ajax
                });
                event.preventDefault(); // prevent submit
            });
        });

用这种方式你可以发送所有的表格。即使您发送1或20个值,也不会发生任何更改。

我怀疑问题出在您的
数据声明上
.serialize()
返回一个字符串,该字符串包含输入字段名及其值。您的
data
语句构成此字符串
cust=cust[]=1&cust[]=2&cust[]=3&cust[]=4
(选中所有复选框时)。这(我在这里只是猜测)首先将数组
cust[]
分配给变量
cust
=1
在查询字符串中无效),然后开始为该数组赋值。这就是为什么只得到最后三个值

要解决问题,您可以将
数据
声明更改为-

data: $('.add-item input[name="cust[]"]').serialize(),
$cust = $_POST['cust[]'];
并将您的php
$cust
声明更改为-

data: $('.add-item input[name="cust[]"]').serialize(),
$cust = $_POST['cust[]'];
如果这在php中有效。(我对php几乎没有经验)

下面演示了
serialize
语句正在创建的内容-

$(“#序列化”)。单击(函数(){
$(“#结果”).text(unescape('cust='+$('.add item input[name=“cust[]”]).serialize()+”);
});

连载

为什么不迭代nodelist并通过php发送
JSON
?看看这是给你的提示谢谢你的帮助,我将使用下面的答案