0

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"/>&nbsp; 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;
    }
2

2 Answers 2

1

Give check box a ID

<tr>
<td>Terms & Conditions</td>
<td><input type="checkbox" id="terms" name="terms"/>&nbsp; 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>

Live Example

Sign up to request clarification or add additional context in comments.

5 Comments

where i should put your code? In regProcess.php or html page ?
its jquery, so must be client side here! :)
I think he's suggesting that you not use ajax and just make sure the box is checked using javascript.
I've separate php page which process all the error an showing all error with echo json_encode($msg); to html page. So now what should i do now ?
Its client side validation. using jquery. you can use serverside also
0

$_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'])) {

3 Comments

The reason your php script isn't returning an error is because you are checking to see if $_POST['terms'] is set, it is not. See my updated answer.
Oh great you answer is working. but I don't understand why you use on ?
You could probably leave that out and just use if (empty($_POST['terms'])) { but web browsers will send the string 'on' as the value of a checked checkbox if you don't specify your own value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.