[jQuery] Check box's as Array - jQuery ajax post
$.validator.setDefaults({
submitHandler: function(){
var checkedregions = new Array()
var checkedtopics = new Array()
if($("input.regionschk:checked").length > 0)
{
$("input.regionschk:checked").each(function(){
checkedregions.push($(this).val());
});
}
if($("input.topicschk:checked").length > 0)
{
$("input.topicschk:checked").each(function(){
checkedtopics.push($(this).val());
});
}
$.post("success.php",
{
'userregions[]': checkedregions,
'usertopics[]': checkedtopics,
},
function(data){
$("#successmsg").html(data);
}
);
return false;
}
});
html:
<input type="checkbox" class="regionschk" name="regions[]"
value="regions1" /> regions2<br />
<input type="checkbox" class="regionschk" name="regions[]"
value="regions2"/> regions2<br />
<input type="checkbox" class="regionschk" name="regions[]"
value="regions3"/> regions3<br />
<input type="checkbox" class="topicschk" name="topics[]"
value="topic" /> topic<br />
<input type="checkbox" class="topicschk" name="topics[]"
value="topic2" /> topic2<br />
<input type="checkbox" class="topicschk" name="topics[]"
value="topic3" /> topic3<br />
<input type="checkbox" class="topicschk" name="topics[]"
value="topic4" /> topic4<br />
PHP:success.php
$regions = $_POST['userregions'];
while (list ($key,$val) = @each ($regions)) {
print $val;
}
$topics = $_POST['usertopics'];
while (list ($key,$val) = @each ($topics)) {
print $val;
}