0

I've scoured the entire internet trying to find a solution to what I'm missing here (or doing wrong). My form doesn't validate even when the check box is checked. Everything else works fine.

It's that checkbox I can't get to work right.

I've tried many different ideas, but it won't validate even with the "terms" checked (such as this example below).

Here's my HTML:

<div class="form-check">
      <input type="checkbox" class="form-check-input" name="terms" id="terms"  />
     <label class="form-check-label" for="terms"><p>I agree to terms of service.</p></label>
</div>

Here's my entire PHP validation (updated with the comments/answers below):

<?php

if(!$_POST) exit;

// Email address verification.
function isEmail($email) { // lots of email validation stuff in here // }

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name     = $_POST['name'];
$email    = $_POST['email'];
$website   = $_POST['website'];
$subject  = $_POST['subject'];
$comments = $_POST['comments'];
$terms = $_POST['terms'];


//set an error counter to trigger the `exit`
$error_counter = 0;
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name</div>';
$error_counter++;
} 
if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email 
address.</div>';
$error_counter++;
}
if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have entered an invalid e- 
mail address, try again.</div>';
$error_counter++;
} 
if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
$error_counter++;
}
if(trim($comments) == '') {
echo '<div class="error_message">Attention! Please enter your message. 
</div>';
$error_counter++;
}
if(empty($terms)) {
echo '<div class="error_message">Attention! Please agree to our terms of 
service.</div>';
$error_counter++;
}
//if `$error_counter > 0 > 0` it will trigger the `exit()` to stop the script and display the errors.
if($error_counter > 0){
exit();
}

if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}


// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "[email protected]";

$address = "[email protected]";


// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."

// Example, $e_subject = '$name . ' has contacted you via Your Website.';

$e_subject = 'You have been contacted by ' . $name . '.';


// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

$e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email ";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

// Email has sent successfully, echo a success page.

echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";

} else {

echo 'ERROR!';

}

?>
1
  • Your validation would end the moment one if returns true. It's also worth noting that you exit every if-else statements. What you should do here instead is to have individual if-else statements to check for validation. Commented May 15, 2018 at 1:54

3 Answers 3

1

You don't set any value attribute for your checkbox :

<input type="checkbox" class="form-check-input" name="terms" id="terms" value="yes" />

More informations : https://developer.mozilla.org/fr/docs/Web/HTML/Element/Input/checkbox

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

2 Comments

Browsers will typically give it a default value (like "on") if there is no value attribute set. stackoverflow.com/questions/12911787/…
I've tried with and without the value attribute. Same problems.
0

I can see your checkbox has no value so the PHP part is getting a garbage/empty value.

In short, just add a required attribute in your HTML side. No need of a check at the server end

<div class="form-check">
      <input type="checkbox" class="form-check-input" name="terms" id="terms" required/>
     <label class="form-check-label" for="terms"><p>I agree to terms of service.</p></label>
</div>

2 Comments

I tried it with a value="true" in the HTML. And I changed the validation to if(($terms) != 'true') { etc....} and it still doesn't work correctly.
the 'required' works okay, but I was hoping to figure out what I'm doing wrong.
0

The problem with your current validation is that you're using if-else wrongly and that you're using exit on every statement which will terminate your script prematurely.

You can do this instead:

For Terms:
<input type="checkbox" class="form-check-input" name="terms" id="terms" value="1"/>    

//set an error counter to trigger the `exit`
$error_counter = 0;
if(trim($name) == '') {
    echo '<div class="error_message">Attention! You must enter your name</div>';
    $error_counter++;
} 
if(trim($email) == '') {
    echo '<div class="error_message">Attention! Please enter a valid email 
address.</div>';
    $error_counter++;
}
if(!isEmail($email)) {
    echo '<div class="error_message">Attention! You have entered an invalid e- 
mail address, try again.</div>';
    $error_counter++;
} 
if(trim($subject) == '') {
    echo '<div class="error_message">Attention! Please enter a subject.</div>';
    $error_counter++;
}
if(trim($comments) == '') {
    echo '<div class="error_message">Attention! Please enter your message.</div>';
    $error_counter++;
}
if(empty($terms)) {
    echo '<div class="error_message">Attention! Please agree to our terms of service.</div>';
    $error_counter++;
}
//if `$error_counter > 0 > 0` it will trigger the `exit()` to stop the script and display the errors.
if($error_counter > 0){
    exit();
}

There's a more beautiful approach to do this. Feel free to add suggestions for the OP.

2 Comments

Thanks. But... the checkbox still won't validate "true" even when the checkbox is checked. I updated my question including the entire PHP with the email configurations. Maybe there's something else in there that I'm missing.
I decided to set the value for your checkbox as it's good practice to never let that hanging with no values. You can try that instead

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.