0

I have many checkbox and other input like text,select ,textarea ,... in my form
I post all data of inputs but checkbox not post data
I saw this link
In Laravel 5.1 Can't post value of checkbox But I do not want to use hidden button
Is there another way ?????
My form

 <form id='test-form'>
 <input name='text1'>
 <input name='text2'>
 <textarea name='text3'></textarea>
 <input   type="checkbox" name="test1"    >
 <input   type="checkbox" name="test2"    >
 <input   type="checkbox" name="test3"    >
 <input   type="checkbox" name="test4"    >
 <input  type="checkbox" name="test5"    >
 <input   type="checkbox" name="test6"    >
 <input type="button" id='send'>
 </form> 
 <script>
 $(document).on("click", "#send", function () {
    $.ajax({
       type: 'post',
        url: 'add',
        data: $("#test-form").serialize(),
       success: function (result) {
            $('#ajax_div').html(result);
       },
   })
})
</script>
Community
  • 1
  • 1
paranoid
  • 6,799
  • 19
  • 49
  • 86

1 Answers1

1

Your checkboxes will not send any data because they do not have the value attribute.

Change your html to:

 <input   type="checkbox" name="test[]" value="1"   >
 <input   type="checkbox" name="test[]" value="2"   >
 <input   type="checkbox" name="test[]" value="3"  >
 <input   type="checkbox" name="test[]" value="4"   >
 <input  type="checkbox" name="test[]"  value="5"  >
 <input   type="checkbox" name="test[]"   value="6"  >
 <input type="button" id='send'>

Note that I also changed the names to test[], this will send an array of all the checkboxes that are checked.

For example: I check checkbox 1 and checkbox 3, the value that the server will receive is in $_REQUEST['test'] (either POST or GET), and will be an array with content [1,3].

If none of the checkboxes is marked, the $_REQUEST['test'] will be not set which you can check server side using isset().

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62