-1

How can I send all the checked item into php using AJAX. The info I expect to send including checkbox id and checkbox inner text. e.g. If I check box1 and box2, it could save the useful info into Data then sent to php when I click submit.

I try to use serialize() in javascript, however the Date has always been "".

var Data = $('#myform').serialize(); 

Please notice that I don not expect realtime respond when I check each box. I just wanna send the entire checked list after I click "submit".

<form class="myform" id="myform" action="add.php">
  <input type="checkbox" class="checkBox[]" id="1"> 1
  <input type="checkbox" class="checkBox[]" id="2"> 2
  <input type="checkbox" class="checkBox[]" id="3"> 3
  <input type="submit" class="add" id="add" name="add">
<form>
ahhfzrx
  • 75
  • 1
  • 6

1 Answers1

1

Simply you can use this code

var checkBox= new Array();
$("input:checked").each(function() {
   data['checkBox[]'].push($(this).val());
});
Mukesh Jakhar
  • 309
  • 2
  • 7
  • If I have two forms formA and formB, both have input checkbox. $("input:checked") will get all checked items from these two forms. How to deal with these two checked list respectively. e.g. I just wanna get the checked item list from formB. – ahhfzrx May 19 '18 at 14:20
  • here i am using name attribute, so you can give different name of check box for formA as well as fromB – Mukesh Jakhar May 19 '18 at 15:18