title: PHP, trying to add captcha to existing form
body: <p>I am not a PHP programmer, but have used it a touch, enough to put in a contact form.  However, I am trying to add a captcha field, which now works but the form does not validate it - so it submits no matter what</p>
  
  <p>Can anybody help please? sorry if the code is messy and thanks in advance</p>
  
  <p>code at the top of my page</p>
  
  <pre><code>        &lt;?php session_start() ?&gt; 
  &lt;?php  
        //If the form is submitted  
      if(isset($_POST['submit'])) {  
  
      //Check to make sure that the name field is not empty  
       if(trim($_POST['name']) == '') {  
           $hasError = true;  
       } else {  
           $name = trim($_POST['name']);  
       }  
  
         //Check to make sure that the subject field is not empty  
      if(trim($_POST['subject']) == '') {  
           $hasError = true;  
       } else {  
           $subject = trim($_POST['subject']);  
       }  
  
      //Check to make sure sure that a valid email address is submitted  
       if(trim($_POST['email']) == '')  {  
           $hasError = true;  
       } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) 
       {  
           $hasError = true;  
       } else {  
           $email = trim($_POST['email']);  
       }  
  
      //Check to make sure comments were entered  
      if(trim($_POST['message']) == '') {  
           $hasError = true;  
       } else {  
      if(function_exists('stripslashes')) {  
           $message = stripslashes(trim($_POST['message']));  
       } else {  
           $message = trim($_POST['message']);  
       } 
  
       /*captcha 2*/ 
  
      if(isset($_POST["captcha"])) {
          $hasError = true;
      } else {
      if($_SESSION["captcha"]==$_POST["captcha"]) {
      }
      }
      //CAPTHCA is valid; proceed the message: save to database, send by e-mail ...
  
      //If there is no error, send the email  
       if(!isset($hasError)) {  
           $emailTo = 'email address'; //Put your own email address here  
           $emailTo = 'email address'; //Put your own email address here  
           $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $message";  
           $headers = 'From: website form &lt;'.$emailTo.'&gt;' . "\r\n" . 'Reply-To: ' .         
           $email;  
  
      mail($emailTo, $subject, $body, $headers);  
           $emailSent = true;  
       }
       }
  ?&gt; 
  
     Code in the form:
  
      [php]&lt;?php if(isset($hasError)) { //If errors are found ?&gt;  
  
          &lt;p class="error"&gt;Please check if you've filled all the fields with valid information.           Thank    you.&lt;/p&gt;  
    &lt;?php } ?&gt;  
  
    &lt;?php if(isset($emailSent) &amp;&amp; $emailSent == true) { //If email is sent ?&gt;  
        &lt;p&gt;&lt;strong&gt;Email Successfully Sent!&lt;/strong&gt;&lt;/p&gt;  
        &lt;p&gt;Thank you &lt;strong&gt;&lt;?php echo $name;?&gt;&lt;/strong&gt; for contacting us. Your email was successfully sent and we will be in touch with you soon.&lt;/p&gt;  
        &lt;?php } ?&gt;  
  
   &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" id="contactform"&gt;  
       &lt;div&gt;  
  
           &lt;p&gt;
           &lt;label for="name"&gt;Name&lt;/label&gt;&lt;br /&gt;
           &lt;input type="text" name="name" value="" id="name" class="required"&gt;
           &lt;/p&gt; 
       &lt;/div&gt;  
  
       &lt;div&gt;  
  
           &lt;p&gt;
           &lt;label for="email"&gt;Email&lt;/label&gt;&lt;br /&gt;
           &lt;input type="text" name="email" value="" id="email" class="required"&gt;
           &lt;/p&gt; 
       &lt;/div&gt;  
  
       &lt;div&gt;  
  
           &lt;p&gt;
           &lt;label for="subject"&gt;Subject&lt;/label&gt;&lt;br /&gt;
           &lt;input type="text" name="subject" value="" id="subject" class="required"&gt;
           &lt;/p&gt; 
  
       &lt;/div&gt;  
  
       &lt;div style="margin-bottom:25px;"&gt;  
  
           &lt;p&gt;
           &lt;label for="message"&gt;Message&lt;/label&gt;&lt;br /&gt;
           &lt;textarea rows="5" name="message" value="" id="message" class="required"&gt;&lt;/textarea&gt;
           &lt;/p&gt; 
  
       &lt;/div&gt;  
       &lt;div style="margin-bottom:25px;"&gt;  
  
  
           &lt;img src="captcha.php" alt="captcha image"&gt;
           &lt;p&gt;
           &lt;label for="captcha"&gt;(antispam code, 3 black symbols)&lt;/label&gt;&lt;br /&gt;
           &lt;input type="text" name="captcha" maxlength="3" id="captcha" class="required"&gt;
           &lt;/p&gt; 
  
       &lt;/div&gt;  
       &lt;input type="submit" value="Send Message" name="submit" /&gt;  
   &lt;/form&gt;
   [/php]
  </code></pre>