Submit form via AJAX with incrementing checkbox id's
I have an AJAX form submission working properly but I have incrementing id's to my checkboxes that I need to be submitted according to what check boxes are checked, then when the user submits the checked selections the data pass over to my PHP script.
How do I pass all checked checkboxes over to my PHP script via AJAX?
$(function () {
$(".submit").开发者_高级运维click(function () {
var pageReturn = $("#pageReturn").val();
var sendCheck = $("#sendCheck").val();
var dataString = 'pageReturn=' + pageReturn + '&sendCheck=' + sendCheck;
if (emailLink == '' || pageReturn == '' || sendCheck == '') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "phpscripts/compose.php?artid=<?php echo $artid; ?>&userart=<?php echo $_GET['userart'];?>",
data: dataString,
cache: false,
success: function () {
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
To pass all check boxes would I add +1 to where my checkboxes are being passed like:
var sendCheck = $("#sendCheck").val()+1;
My HTML code for my check boxes looks like this
<input name="sendCheck" id="sendCheck<?php echo $i++; ?>" class="reccomendCheck" type="checkbox" value="<?php echo $row_ArtContact['contact_id'];?>" />
Check boxes with incrementing values that need to be passes over to my PHP script.
In theory there could be an unlimited number of checkboxes that the user could randomly check and need to be summited.
Use the serialize
function to generate your data.
Look at this example: click a few checkboxes and see what happens!
http://jsfiddle.net/VeBDk/
Example:
HTML:
<form>
Check 1<input type="checkbox" name="check1" id="check1" value="1"/>
Check 2<input type="checkbox" name="check2" id="check2" value="2"/>
Check 3<input type="checkbox" name="check3" id="check3" value="3"/>
Check 4<input type="checkbox" name="check4" id="check4" value="4"/>
Check 5<input type="checkbox" name="check5" id="check5" value="5"/>
<button>Submit</button>
</form>
jQuery:
$(document).ready(function(){
var data = $('form').serialize();
$.post('script.php', data, function(){
//success!
});
});
精彩评论