title: Contact form with math captcha
body: <p>I've successfully created a contact form with php that gives the various required messages.    Now I would like to add a simple random arithmetic captcha (non-image).  See the anonymised (working) html form and existing (working, but without arithmetic captcha) php below.</p>
  
  <p>The idea is to show "Incorrect answer" in the same way as the other error messages, or to pass to the Thankyou page for a correctly filled out form with correct answer.  I've had a good go at this but can't quite get it to work.  Any assistance much appreciated.</p>
  
  <hr>
  
  <p>HTML:</p>
  
  <pre><code>    &lt;p&gt;Area of interest:
      &lt;input type="radio" name="likeit" value="A" checked="checked" /&gt; A
      &lt;input type="radio" name="likeit" value="B" /&gt; B
      &lt;input type="radio" name="likeit" value="C" /&gt; C&lt;/p&gt;
  
      &lt;p&gt;How did you hear about us?
      &lt;select name="how"&gt;
      &lt;option value=""&gt; -- Please select -- &lt;/option&gt;
      &lt;option&gt;Recommendation&lt;/option&gt;
      &lt;option&gt;Internet&lt;/option&gt;
      &lt;option&gt;Advertisement&lt;/option&gt;
      &lt;option&gt;Other&lt;/option&gt;
      &lt;/select&gt;&lt;/p&gt;
  
      &lt;p&gt;&lt;strong&gt;Your message subject:&lt;/strong&gt;&lt;br /&gt;&lt;input type="text" name="subject" size="35"/&gt;&lt;/p&gt;
  
      &lt;p&gt;&lt;strong&gt;Your message:&lt;/strong&gt;&lt;br /&gt;
      &lt;textarea name="comments" rows="10" cols="40"&gt;&lt;/textarea&gt;&lt;/p&gt;
  
      &lt;p&gt;Please answer the following arithmetic question: What is &lt;?php echo $digit1;?&gt; + &lt;?php echo $digit2;?&gt;?
       &lt;input name="captcha" type="text" size="2" id="captcha"/&gt;&lt;/p&gt;
  
  
      &lt;p&gt;&lt;input type="submit" value="Send" /&gt;&lt;/p&gt;
  
      &lt;/form&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>    &lt;?php
      /* Contact form with arithmetic captcha */
      $myemail  = "enquiries@X.co.uk";
  
      /* Check all form inputs using check_input function */
      $yourname = check_input($_POST['yourname'], "Enter your name");
      $email    = check_input($_POST['email']);
      $telephone  = check_input($_POST['telephone']);
      $website  = check_input($_POST['website']);
      $likeit   = check_input($_POST['likeit']);
      $how_find = check_input($_POST['how']);
      $subject  = check_input($_POST['subject'], "Add a subject");
      $comments = check_input($_POST['comments'], "Add your message");
  
      /* If e-mail is not valid show error message */
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
      {
              show_error("Email address is not valid");
      }
  
      /* If URL is not valid set $website to empty */
      if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
      {
          $website = '';
      }
  
      /* Message for the email */
      $message = "Hello!
  
      Your contact form has been submitted by:
  
      Name: $yourname
      Email: $email
      Telephone: $telephone
      URL: $website
  
      Area of interest? $likeit
      How did they find us? $how_find
  
      Comments:
      $comments
  
      End of message
      ";
  
      /* Send the message using mail() function */
      mail($myemail, $subject, $message);
  
      /* Redirect visitor to the thankyou page */
      header('Location: thankyou.html');
      exit();
  
      /* Functions used */
      function check_input($data, $problem='')
      {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          if ($problem &amp;&amp; strlen($data) == 0)
          {
              show_error($problem);
          }
          return $data;
      }
  
      function show_error($myError)
      {
      ?&gt;
  
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
      &lt;!--
      Head data in here
      --&gt;
          &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  
          &lt;body&gt;
  
              &lt;div id="mainheader"&gt;
  
      &lt;div id="mainlogo"&gt;
  
      &lt;h1&gt;&lt;a href="http://www.X.co.uk/" title="X"&gt;
                                      &lt;img style="border:0;width: 260px; height: 160px;" src="images/X.jpg" alt="X" /&gt;&lt;/a&gt;&lt;/h1&gt;
      &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div id="content"&gt;
              &lt;div class="content"&gt;
                      &lt;h2 class="title"&gt;Error!&lt;/h2&gt;
  
      &lt;p&gt;&lt;strong&gt;Please correct the following error:&lt;/strong&gt;&lt;/p&gt;
      &lt;p&gt;&lt;?php echo $myError; ?&gt;&lt;/p&gt;
  
              &lt;/div&gt;
      &lt;/div&gt;
  
          &lt;div id="panel"&gt;
  
              &lt;div id="main" class="boxed"&gt;
                  &lt;h2 class="heading"&gt;Main&lt;/h2&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;a href="index.html"&gt;Home&lt;/a&gt; &lt;/li&gt;
                      &lt;li&gt;&lt;a href="about.html"&gt;About&lt;/a&gt; &lt;/li&gt;
                      &lt;li&gt;&lt;a href="contact.html"&gt;Contact&lt;/a&gt; &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
  
              &lt;div id="services" class="boxed"&gt;
                  &lt;h2 class="heading"&gt;Services&lt;/h2&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;a href="services.html"&gt;Services&lt;/a&gt; &lt;/li&gt;
                      &lt;li&gt;&lt;a href="recent-projects.html"&gt;Recent projects&lt;/a&gt; &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
  
              &lt;div id="pricing" class="boxed"&gt;
                  &lt;h2 class="heading"&gt;Pricing&lt;/h2&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;a href="pricing.html"&gt;Pricing&lt;/a&gt; &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
  
              &lt;div id="info" class="boxed"&gt;
                  &lt;h2 class="heading"&gt;Info&lt;/h2&gt;
                  &lt;ul&gt;
                      &lt;li&gt;&lt;a href="tips-and-tricks.html"&gt;Tips and tricks&lt;/a&gt; &lt;/li&gt;
                      &lt;li&gt;&lt;a href="useful-links.html"&gt;Useful links&lt;/a&gt; &lt;/li&gt;
                      &lt;li&gt;&lt;a href="faq.html"&gt;Frequently asked questions&lt;/a&gt; &lt;/li&gt;
                      &lt;li&gt;&lt;a href="site-map.html"&gt;Site map&lt;/a&gt; &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
  
                      &lt;div id="contact" class="boxed"&gt;
                              &lt;h2 class="heading"&gt;Contact&lt;/h2&gt;
                              &lt;ul&gt;
                              &lt;li&gt;&lt;a href= "mailto:enquiries&amp;#38;#64;X.co.uk"&gt;Contact by email&lt;/a&gt; &lt;/li&gt;
                              &lt;li&gt;&lt;strong&gt;Telephone:&lt;br /&gt;X&lt;/strong&gt; &lt;/li&gt;
                              &lt;/ul&gt;
                      &lt;/div&gt;
  
          &lt;/div&gt;
  
         &lt;div id="mainfooter"&gt;
                      &lt;p&gt; &amp;#38;#169; 2011 X&lt;br /&gt;Designed by &lt;a href="http://www.X.co.uk/" title="X"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/a&gt; &lt;/p&gt;
                              &lt;a href="http://validator.w3.org/check?uri=referer" title="Valid XHTML 1.0"&gt;
                      &lt;img style="border:0;width:88px;height:31px" src="images/valid-xhtml10.png" alt="Valid XHTML 1.0" /&gt;
                  &lt;/a&gt;
                              &lt;a href="http://jigsaw.w3.org/css-validator/check/referer" title="Valid CSS!"&gt;
                      &lt;img style="border:0;width:88px;height:31px" src="images/vcss.gif" alt="Valid CSS!" /&gt;
                  &lt;/a&gt;
          &lt;/div&gt;
  
          &lt;/body&gt;
          &lt;/html&gt;
      &lt;?php
      exit();
      }
      ?&gt;
  </code></pre>