Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send value of checked checkboxes to another page with jquery ajax

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.

like image 330
H Emad Avatar asked Dec 31 '25 18:12

H Emad


2 Answers

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.

like image 67
Shyju Avatar answered Jan 02 '26 10:01

Shyju


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(){...});
like image 35
bitoshi.n Avatar answered Jan 02 '26 09:01

bitoshi.n



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!