I am having difficulty retrieving my checkbox array data in my called PHP from an ajax send of a serialized form. I can call up all of the other form fields, but not the checkbox array. Can anyone help? I'm pulling my hair out here. :(
My Form:
<form id="registration" method="POST" action="">
<input type="text" id="pFirst" name="pFirst" class="form-control" placeholder="First">
<input type="text" id="pLast" name="pLast" class="form-control" placeholder="Last">
<input type="checkbox" name="class[]" value="1">
<input type="checkbox" name="class[]" value="2">
<input type="checkbox" name="class[]" value="3">
<input type="checkbox" name="class[]" value="4">
<button type="submit">Register</button>
</form>
My ajax:
function OnSubmit(form){
$.ajax({
url:'processRegistration.php',
type:'post',
data: $(form).serializeArray(),
success:function(response){
var msg = "";
if(response == "1"){
msg = "Registration Received";
}else{
msg = "Sorry, something went wrong. Please contact the Web Admin.";
}
//alert(msg);
alert(response);
}
});
}
My PHP receiver:
echo $_POST['pFirst'].', '.$_POST['pLast'].', '.$_POST['class'];
In my Ajax, I alerted the response so I could see what is returned. If I leave the echo in the receiver the way I show it in my code, I get the error "Undefined array key 'class'". If I leave the $_POST['class'] off, I get the first and last names. I also looked at this solution:Send array with checked Checkbox value and retrieve as string in php but it uses JSON.stringify() and I want to keep the form data in URL encoded format.
What I need to happen is to be able to access the checked checkboxes passed by the serialized form while still being able to call up each passed form field as a $_POST item.