Send multiple checkbox data to PHP via jQuery ajax()

后端 未结 6 1073
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 08:31

I want to submit a POST form that contains a textarea field and an input field(s) (type=\"checkbox\" with an arbitrary/variable number of checkboxes) on my website via jQuer

相关标签:
6条回答
  • 2020-11-28 09:12
    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

    0 讨论(0)
  • 2020-11-28 09:17

    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

    0 讨论(0)
  • 2020-11-28 09:21

    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']);

    0 讨论(0)
  • 2020-11-28 09:27

    The code you have at the moment seems to be all right. Check what the checkboxes array contains using this. Add this code on the top of your php script and see whether the checkboxes are being passed to your script.

    echo '<pre>'.print_r($_POST['myCheckboxes'], true).'</pre>';
    exit;
    
    0 讨论(0)
  • 2020-11-28 09:27

    You may also try this,

    var arr = $('input[name="myCheckboxes[]"]').map(function(){
      return $(this).val();
    }).get();
    
    console.log(arr);
    
    0 讨论(0)
  • 2020-11-28 09:28
        $.post("test.php", { 'choices[]': ["Jon", "Susan"] });
    

    So I would just iterate over the checked boxes and build the array. Something like.

           var data = { 'user_ids[]' : []};
            $(":checked").each(function() {
           data['user_ids[]'].push($(this).val());
           });
            $.post("ajax.php", data);
    
    0 讨论(0)
提交回复
热议问题