Jquery帖子不工作,通过jquery发送多个选中的复选框到php脚本

时间:2020-11-29 13:41:26

In the php script when I echo the $_POST['AllItems '] then it shows only the last selected value instead of a whole string of values or array.

在PHP脚本中,当我回显$ _POST ['AllItems']时,它只显示最后选择的值而不是整个值的数组或数组。

Below is the javascript am using :

以下是我正在使用的javascript:

$(function () {
    $('#senditems').click(function () {
        var items = $('input[name^="item"]:checked').map(function () {
            return this.value;
        }).get();

*****************************
  //here when i do alert(items); ,
 then it shows comma separated values - 1,22,321
****************************


        $.post("saveitems.php", {
            AllItems: items
        });
    });
});

Form is :

表格是:

    <form>

    <input type="checkbox" name="items[1]" value="1" />
<input type="checkbox" name="items[22]" value="22" />
<input type="checkbox" name="items[321]"  value="321" />

    </form>

1 个解决方案

#1


2  

This is how your form should look.

这就是表单的外观。

<form id="my_form">

  <input type="checkbox" name="items[]" value="1" />
  <input type="checkbox" name="items[]" value="22" />
  <input type="checkbox" name="items[]"  value="321" />

</form>

Then in your php, you can iterate through all the $_POST['items']

然后在你的PHP中,你可以遍历所有$ _POST ['items']

If you want to use jQuery to post, you can use this, but is not necessary as you can add a method and action to the html in your form:

如果你想使用jQuery发布,你可以使用它,但没有必要,因为你可以在表单中的html中添加方法和操作:

$('#senditems').on('click', function(){
    var form_data = $('#my_form').serialize();

    $.post({
        url: "your_php.php",
        data: form_data
    }); 
});

#1


2  

This is how your form should look.

这就是表单的外观。

<form id="my_form">

  <input type="checkbox" name="items[]" value="1" />
  <input type="checkbox" name="items[]" value="22" />
  <input type="checkbox" name="items[]"  value="321" />

</form>

Then in your php, you can iterate through all the $_POST['items']

然后在你的PHP中,你可以遍历所有$ _POST ['items']

If you want to use jQuery to post, you can use this, but is not necessary as you can add a method and action to the html in your form:

如果你想使用jQuery发布,你可以使用它,但没有必要,因为你可以在表单中的html中添加方法和操作:

$('#senditems').on('click', function(){
    var form_data = $('#my_form').serialize();

    $.post({
        url: "your_php.php",
        data: form_data
    }); 
});