3

I use $.get(... syntax to send my form data with Jquery Ajax.
I want to know how can I send value of checked checkboxes.
For example I use $("#myinput").val() to send an inputbox value to my php page.

bitoshi.n
  • 2,278
  • 1
  • 16
  • 16
H Emad
  • 329
  • 8
  • 19
  • [Here](http://stackoverflow.com/questions/4813219/jquery-checkbox-value) is a lot of information on jQuery checkbox values. – jeffjenx May 08 '12 at 16:38

2 Answers2

2

You can use serialize() or serializeArray() to your form. example:

var data = $('yourform').serialize();

or

var data = $('yourform').serializeArray();

then put the data into ajax

$.get('link.php',data,function(){...});
bitoshi.n
  • 2,278
  • 1
  • 16
  • 16
2

I dont know how your HTML looks like. Here is a generic solution. You may customize this to your specific needs

Assuming you have some HTML markup like this

Item 11<input type="checkbox" id="chk1" value="11" class="chkItem"  /><br/>
Item 12<input type="checkbox" id="chk2" value="12" class="chkItem"  /><br/>
Item 14<input type="checkbox" id="chk4" value="14" class="chkItem"  /><br/>
<input type="submit" value="Save" id="btnSave" />

And the script is

$(function(){
   $("#btnSave").click(function(e){
        e.preventDefault();
        var items=$("input[type='checkbox']:checked").map(function () {
            return this.value;
        }).get().join(',');

        $.post("yoururl.php?data="+items,function(response){
         //alert(response);
        });        
   });           
});

The above script will take all checked input elements and build a string with the selected items value seperated by comma and send it in the query string. You can read this in your php file and split by comma and read the values.

Shyju
  • 214,206
  • 104
  • 411
  • 497