In my html form all data is successfully validation except checkbox value. It's not validate with jquery/ajax request.
here is my html form ajax and html code (only terms part):
<tr>
<td>Terms & Conditions</td>
<td><input type="checkbox" name="terms"/> I agree to your <a href="">terms and
conditions</a>.</td>
<script>
$('#form1').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'regProcess.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#info1').html('');
$.each( data, function( key, value ) {
$('#info1').append('<p>'+value+'</p>');
});
}
});
});
</script>
In regProcess.php page following is my Checkbox validation code but it's not validating..
if(isset($_POST['terms']) && $_POST['terms'] == ""){
$msg[] = "You must agree our terms and conditions";
$msg1['error'] = true;
}
$_POST['terms']
will not be set if it's an unchecked checkbox. It should instead be:
if (empty($_POST['terms']) || $_POST['terms'] !== 'on') {
or just
if (empty($_POST['terms'])) {
Give check box a ID
<tr>
<td>Terms & Conditions</td>
<td><input type="checkbox" id="terms" name="terms"/> I agree to your <a href="">terms and
conditions</a>.</td>
Then use this to find out checkbox is checked or not??
<script>
$('#form1').submit(function(event) {
event.preventDefault();
if($('#terms').is(':checked') )
{
$.ajax({
type: 'POST',
url: 'regProcess.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#info1').html('');
$.each( data, function( key, value ) {
$('#info1').append('<p>'+value+'</p>');
});
}
});
}
else{
alert("Check terms and condition");
}
});
</script>