title: PHP contact form verification field
body: <p>Using the script below (taken from a template), I added a simple verification question to prevent spam (What is 2 + 2?). As of now, when the submit button is filled, the error.php page appears but the form is submitted as blank. </p>
  
  <p>How do I ensure the form is only submitted after verifying the security question? </p>
  
  <pre><code>&lt;?php
  
  $EmailTo = "myemail@email.com";
  $Subject = "website";
  $name = Trim(stripslashes($_POST['name'])); 
  $email = Trim(stripslashes($_POST['email'])); 
  $number = Trim(stripslashes($_POST['number'])); 
  $message = Trim(stripslashes($_POST['message'])); 
  
  // validation
  $validationOK=true;
  if (!$validationOK) {
    print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=error.php\"&gt;";
    exit;
  }
  
  // prepare email body text
  $Body = "";
  $Body .= "Name: ";
  $Body .= $name;
  $Body .= "\n";
  $Body .= "Tel: ";
  $Body .= $number;
  $Body .= "\n";
  $Body .= "Email: ";
  $Body .= $email;
  $Body .= "\n";
  $Body .= "Message: ";
  $Body .= $message;
  $Body .= "\n";
  
  // send email 
  $submit = mail($EmailTo, $Subject, $Body, "From: &lt;$email&gt;");
  
  // redirect to success page 
  if(isset($_POST['submit'])){
  if(isset($_POST['answer']) &amp;&amp; $_POST['answer'] == 4){
    print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\"&gt;";
  }else{
    print "&lt;meta http-equiv=\"refresh\" content=\"0;URL=error.php\"&gt;";
  }
  }
  ?&gt;
  </code></pre>
  
  <p>The form code follows:</p>
  
  <pre><code>&lt;div id="form-holder" class="cont"&gt;
                          &lt;h2&gt;Contact form&lt;/h2&gt;
                      &lt;form method="post" action="send.php" class="uniform get-in-touch"&gt;
  
                      &lt;div class="i-h"&gt;
                      &lt;input type="text" name="name" id="name"/&gt;
                      &lt;div class="i-l"&gt;&lt;span&gt;Name&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
  
                      &lt;div class="i-h"&gt;
                      &lt;input type="email" name="email" id="email"/&gt;
                      &lt;div class="i-l"&gt;&lt;span&gt;E-mail&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
  
                      &lt;div class="i-h"&gt;
                      &lt;input type="text" name="number" id="number"/&gt;
                      &lt;div class="i-l"&gt;&lt;span&gt;Number&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
  
                      &lt;div class="t-h"&gt;
                      &lt;textarea name="message" rows="32" cols="8" id="message" placeholder="Please leave us a message."&gt;&lt;/textarea&gt;
                      &lt;/div&gt;
  
                      &lt;br&gt;&lt;p&gt;What is 2+2?&lt;/p&gt;&lt;input type="text" name="answer" id="answer" /&gt;
  
                      &lt;input type="submit" name="submit" value="Submit" class="submit-button"&gt;
                              &lt;a href="#" class="do-clear"&gt;Clear&lt;/a&gt;
                  &lt;/form&gt;
                      &lt;/div&gt;
  </code></pre>