How to send the values of an array of checkboxes through Ajax using jQuery?

how to pass multiple checkbox value in ajax jquery
how to pass multiple array data in jquery ajax
how to pass multiple checkbox value in ajax jquery mvc
jquery ajax pass array to controller
add and remove checkbox values to array jquery
pass array in ajax javascript
passing multiple checkbox values ajax
how to send array in ajax response

I have a form with a lot of form fields (12 x n rows). The first field in each row (which represents a product) is a checkbox that resembles this:

<input type="checkbox" class="ids" name="ids[]" value="1">

The value of each checkbox is unique.

What I am trying to do is send checked values to a PHP script for processing via Ajax. What I am am having issues with is getting the IDs to the server properly. I have tried using several things including:

$('.ids:checked').serialize();

and

var ids = [];
$('.ids:checked').each(function(i, e) {
    ids.push($(this).val());
});

$.ajax({
    url: "stub",
    type: "post",
    dataType: "json",
    data: {
        'ids[]': 'ids[]='+ids.join('&ids[]=')
    },
    success: function(data) {
        // stub
    }
});

But these both result in getting this on the server:

ids[]=104&ids;[]=105

I could serialize the whole form and send it over but that could result in a lot of data being sent that is going to be unused.

How do I send only the values of delete[] to the server? Ideally in a way that PHP recognizes it as an array?

(I have worked around it by sending the IDs over as a comma delimited string but would like to know how to accomplish this since I spent enough time trying to figure it out).

This worked fine for me

<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
<input type="checkbox" class="ids" name="ids[]" value="5">
<input type="checkbox" class="ids" name="ids[]" value="6">

<div id="response"></div>
<button id="submit">Submit</button>

<script>

$('#submit').click(function() {

$.ajax({
    url: "stub.php",
    type: "post",
    data: $('.ids:checked').serialize(),
    success: function(data) {
    $('#response').html(data);
    }
});


});
</script>

Then on stub.php

var_dump($_POST);

How to Send JavaScript Array to the AJAX using jQuery and PHP, On submit, button click gets input values and initializing the Array lang with checked Duration: 16:01 Posted: Dec 22, 2018 A JavaScript Array can be used to pass the group of related values as data to the $.ajax for processing and get the response. E.g. pass all checked checkboxes values, selected values from the list. In this tutorial, I show how you can pass JavaScript Array to an AJAX request with an example.

Why don't you send the id's as comma separated string. You can split it on server side and apply the logic associated with it..

var ids = [];
$('.ids:checked').each(function(i, e) {
    ids.push($(this).val());
});

$.ajax({
    url: "stub",
    type: "post",
    dataType: "json",
    data: {
        'ids[]': ids.join()
    },
    success: function(data) {
        // stub
    }
});

Passing multiple checkbox data array with jquery and php, Its hard to pass multiple checkbox data to a php via a jquery file, we using jQuery ajax post � Passing array of checkbox values to php through jQuery ( example)� Given a number of checkboxes. The task is to get the value of all selected checkboxes in the array using JQuery. Few of the methods are discussed below: jQuery on() Method This method adds one or more event handlers for the selected elements and child elements. Syntax: $(selector).on(event, childSel, data, fun, map) Parameters:

Solution for me working fine

//get checkbox checked data as the array

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



var dataString = 'ids='+ ids;


 $.ajax({
            type: "POST",
            url: "ajax_fees_pay_Directly.php",
            data: dataString,
            cache: false,
            success: function(data){

}
 });

How to send multiple checkbox group values through ajax?, Send multiple checkbox data to PHP via jQuery ajax(). php, jquery, ajax, checkbox. asked by John Anderson on 04:18AM - 29 Feb 12 UTC. Getting Checked Values using jQuery. This jQuery script is used to get the checked value from the form checkbox field using jQuery each(). Using this jQuery function it runs a loop to get the checked value and put it into an array. Then the selected values with the count will be shown using an alert box.

How to get all selected checkboxes in an array using jQuery , The task is to get the value of all selected checkboxes in the array using JQuery. Few of the If there are multiple event values, separate them by space. Event must be a It specifies additional data to pass to the function. fun: This parameter is required. "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery .min.js" >. This is is the way you can use this function. You can put values in link as variable and catch it with get. You can also send the values through ajax, but in that case you can send form fields as well, so I see no point in that unless you have lot of data.

Passing array of checkbox values to php through jQuery (example , input.html process.php Test-run screenshot: Live demo (Using Passing array of checkbox values to php through jQuery (example) script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" trying to pass the selected checkboxes array to the sresults.php script on� I am trying to get values of all checkboxes that are currently checked and store them into an array. Here is my code so far: $("#merge_button").click(function(event){ event.preventDefault();

Send checkbox status with Ajax / JSON and save it, .prop(). method return boolean type. True or false. You should send checked value and save it into database. And in HTML get your value and� I have define checkboxes with values like how many programming languages you have known, when user select more than one language checkbox then all value of checkbox store into an array using Jquery and I will convert that array to string and by using Jquery ajax method I will insert values of all checkboxs to table by using PHP script.

Comments
  • By chance, are the ids in any way associated with the ids in your database?
  • How about data: {'ids': ids},
  • Looks like I was close but not quite there. Thanks for your help.
  • Wrap all your checkboxes within form tags, then serialize. <form id="myform"></form> ---- myform.serialize().
  • Is it works without Form, And without specifying type: "post", .
  • If you read the bottom of my question you'll see that's what I did to work around this.