title: form validation with captcha script
body: <p>I have created custom contact from with Ajax and implemented in WordPress. Now I want to add custom <code>captcha</code> script. </p>
  
  <p>Currently I am using this captcha script for my contact form <a href="http://www.9lessons.info/2011/01/php-captcha-code.html" rel="nofollow">9lessons phpcaptcha</a> but after implementing I am getting <code>verification Wrong</code> alert box every time. I don't know whats wrong in my code.</p>
  
  <p>FYI: I have created plugin file so I have added all code in one file.</p>
  
  <p><strong>My Code:</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  $cap = 'notEq';
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      if ($_POST['captcha'] == $_SESSION['cap_code']) {
          // Captcha verification is Correct. Do something here!
          $cap = 'Eq';
      } else {
          // Captcha verification is wrong. Take other action
          $cap = '';
      }
  }
  ?&gt;
  
  &lt;?php
      function saleContactForm() {
      ?&gt;
          &lt;form method="post" action="" id="SaleForm"&gt;            
              &lt;ul class="cnt-frm"&gt;
                  &lt;li&gt;&lt;input name="sa_first_name" id="sa_first_name" type="text" value="First Name*" class="sl-txbx" maxlength="20"  onchange="return trim(this)"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input name="sa_last_name" id="sa_last_name" type="text" value="Last Name*" class="sl-txbx" maxlength="20"  onchange="return trim(this)"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input name="sa_company" id="sa_company" type="text" value="Company*" class="sl-txbx" maxlength="20"  onchange="return trim(this)"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input name="sa_email" id="sa_email" type="text" value="Email*" class="sl-txbx"  onchange="return trim(this)"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input name="sa_phone" id="sa_phone" type="text" value="Phone*" class="sl-txbx" maxlength="14"  onchange="return trim(this)"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;textarea name="sa_message" id="sa_message" class="sl-txara" onchange="return trim(this)"&gt;Message*&lt;/textarea&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="hidden" name="action" value="saleAction"/&gt;&lt;/li&gt;
                  &lt;li&gt;
                      &lt;input type="text" name="captcha" id="captcha" maxlength="6" size="6"/&gt;
                      &lt;img src="http://localhost/example/wp-content/plugins/sale/captcha.php"/&gt;
                  &lt;/li&gt;
                  &lt;li&gt;&lt;input type="reset" name="reset" value="reset" id="sale_reset" class="re_none"&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;input type="submit" name="submit" value="submit" class="submit"&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/form&gt;
  
          &lt;script type="text/javascript"&gt;
  
              jQuery('#SaleForm').submit(ajaxSubmit);
  
              function ajaxSubmit(){
  
                  var capch = '&lt;?php echo $cap; ?&gt;';
                  if(capch != 'notEq'){
                      if(capch == 'Eq'){
                          alert("Your form is successfully Submitted ");
                      }else{
                          alert("verification Wrong!");
                          return false;
                      }
                  }                           
  
                  var SaleForm = jQuery(this).serialize();
  
                  jQuery.ajax({
                      type:"POST",
                      url: "&lt;?php echo esc_url( home_url( '/' ) ); ?&gt;wp-admin/admin-ajax.php",
                      data: SaleForm,
                      success:function(data){
                          alert('Thanks! Your request has been sent.');
                          jQuery( "#sale_reset" ).trigger( "click" );
                      },
                      error: function(errorThrown){
                          alert(errorThrown);
                      }  
                  });
  
                  return false;
              }
          &lt;/script&gt;
          &lt;?php
      }
  
  function widget_saleContactForm() {
    saleContactForm();
  }
  
  wp_enqueue_script('jquery');
  
  add_action('wp_ajax_saleAction', 'saleAction');
  function saleAction() {
      $sa_first_name = $_POST['sa_first_name'];
  
      die();
  }
  add_action('wp_ajax_nopriv_saleAction', 'saleAction');
  ?&gt;
  </code></pre>
  
  <p><strong>captcha.php:</strong> </p>
  
  <pre><code>&lt;?php
  session_start();
  $ranStr = md5(microtime());
  $ranStr = substr($ranStr, 0, 6);
  $_SESSION['cap_code'] = $ranStr;
  $newImage = imagecreatefromjpeg("cap_bg.jpg");
  $txtColor = imagecolorallocate($newImage, 0, 0, 0);
  imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
  header("Content-type: image/jpeg");
  imagejpeg($newImage);
  ?&gt;
  </code></pre>
  
  <p>I will appreciate if you help to sort out this issue.</p>
  
  <p>Thanks.  </p>