-1

My goal is to require checkbox to be checked before sending. If checkbox is empty visitor should receive a message. I tried using isset and empty() but it didn't work for me. Where is my mistake? Any ideas?

HTML

<form id="contact-form" name="contact-form" action="mail.php" method="POST" onsubmit="return validateForm()">
  <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input" id="form" name="form" value="1" checked>
    <label for="form" class="custom-control-label underline"></label>
  </div>
  <div class="text-center text-md-left pl-4">
    <a class="btn btn-send" onclick="validateForm()">Send</a>
  </div>
  <div id="status"></div>
</form>

JS

function validateForm() {
  document.getElementById('status').innerHTML = "Sending...";
    formData = {
        'name'     : $('input[name=name]').val(),
        'email'    : $('input[name=email]').val(),
        'subject'  : $('input[name=subject]').val(),
        'message'  : $('textarea[name=message]').val(),
        'form'     : $('input:checkbox[name=form]').is(':checked')
    };


   $.ajax({
    url : "mail.php",
    type: "POST",
    data : formData,
    success: function(data, textStatus, jqXHR)
    {

        $('#status').text(data.message);
        if (data.code) //If mail was sent successfully, reset the form.
        $('#contact-form').closest('form').find("input[type=text], textarea").val("");
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        $('#status').text(jqXHR);
    }
  });
}

mail.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$form = $_POST['form'];

header('Content-Type: application/json');
if($form !== '1'){
    print json_encode(array('message' => 'Checkbox must be checked', 'code' => 0));
  exit();
}
if ($name === ''){
  print json_encode(array('message' => 'Name cannot be empty', 'code' => 0));
  exit();
}
if ($email === ''){
  print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
  exit();
} else {
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
  print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
  exit();
  }
}
if ($subject === ''){
  print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0));
  exit();
}
if ($message === ''){
  print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));
  exit();
}
$content="From: $name \nEmail: $email \nMessage: $message";
$recipient = "mymail@gmail.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
print json_encode(array('message' => 'Email successfully sent!', 'code' => 1));
exit();
?>
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
Jan Ponichter
  • 85
  • 1
  • 1
  • 3
  • 1
    Welcome to SO! Please include your code in the question itself as a [mcve] because links are unreliable and can change over time, rendering the question useless for future visitors. See [ask] and take the tour. Thanks! – ggorlen Nov 06 '19 at 05:58
  • 2
    You can't validate the checkbox using PHP before you send it. You need to use javascript for that. PHP lives on the server while the form exists on the client until you submit the form to the server. Have a look at [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – M. Eriksson Nov 06 '19 at 06:22

1 Answers1

0

Use Javascript Validation

<form action="index.html" onsubmit="return validation()"> 

  <input type="checkbox" id="check">

  <input type="submit" value="Submit">

</form>


<script>
 function validation() {
            if (document.getElementById('check').checked) {
            return true;
        } else {
            alert("checkbox is not checked");
            return false;     

        }

    }

</script>
Pardeep Kumar
  • 83
  • 1
  • 9