18

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).

2
  • By chance, are the ids in any way associated with the ids in your database? Commented Sep 15, 2012 at 0:09
  • How about data: {'ids': ids}, Commented Sep 15, 2012 at 0:24

3 Answers 3

25

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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", .
3

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
    }
});

1 Comment

If you read the bottom of my question you'll see that's what I did to work around this.
2

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){

}
 });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.