Send multiple checkbox data to PHP via jQuery ajax() Send multiple checkbox data to PHP via jQuery ajax() php php

Send multiple checkbox data to PHP via jQuery ajax()


Yes it's pretty work with jquery.serialize()

HTML

<form id="myform" class="myform" method="post" name="myform"><textarea id="myField" type="text" name="myField"></textarea><input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue1" /><input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" /><input id="submit" type="submit" name="submit" value="Submit" onclick="return submitForm()" /></form> <div id="myResponse"></div>

JQuery

function submitForm() {var form = document.myform;var dataString = $(form).serialize();$.ajax({    type:'POST',    url:'myurl.php',    data: dataString,    success: function(data){        $('#myResponse').html(data);    }});return false;}

NOW THE PHP, i export the POST data

 echo var_export($_POST);

You can see the all the checkbox value are sent.I hope it may help you


var myCheckboxes = new Array();$("input:checked").each(function() {   data['myCheckboxes[]'].push($(this).val());});

You are pushing checkboxes to wrong array data['myCheckboxes[]'] instead of myCheckboxes.push


Check this out.

<script type="text/javascript">    function submitForm() {$(document).ready(function() {$("form#myForm").submit(function() {        var myCheckboxes = new Array();        $("input:checked").each(function() {           myCheckboxes.push($(this).val());        });        $.ajax({            type: "POST",            url: "myurl.php",            dataType: 'html',            data: 'myField='+$("textarea[name=myField]").val()+'&myCheckboxes='+myCheckboxes,            success: function(data){                $('#myResponse').html(data)            }        });        return false;});});}</script>

And on myurl.php you can use print_r($_POST['myCheckboxes']);