title: how to validate CAPTCHA using php and jquery
body: <p>I am new to web development and I am trying to put CAPTCHA into my website. I am stuck at this. And I couldn't find any help.</p>
  
  <p>The following is my Form code:</p>
  
  <pre><code>&lt;tr&gt;
      &lt;td&gt;
          &lt;img src="html-contact-form-captcha/captcha_code_file.php?rand=&lt;?php echo rand(); ?&gt;" id='captchaimg' &gt;&lt;br&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td align="right"&gt;&lt;b&gt; Enter Image Text &lt;/b&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input placeholder="Enter the code above here" id="6_letters_code" name="6_letters_code" type="text"&gt;&lt;br&gt;
            &lt;small&gt;Can't read the image? click &lt;a href='javascript: refreshCaptcha();'&gt;here&lt;/a&gt; to refresh&lt;/small&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <p>And on this same page I am trying to validate this CAPTCHA by the following code:</p>
  
  <pre><code>var cde = document.getElementById('6_letters_code');
      if( cde.value == "" ||
          ($_SESSION['6_letters_code'] != $_POST['6_letters_code']) ) {
  
                  alert( "Code Matched!" );
                  //alert( "Code Doesn't Match! \n Code Not Entered!" );
                  return false; 
     }
  </code></pre>
  
  <p>And this is where I am getting my CAPTCHA: (captcha.php)</p>
  
  <pre><code>session_start(); // Staring Session
  $captchanumber = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'; // Initializing PHP variable with string
  $captchanumber = substr(str_shuffle($captchanumber), 0, 6); // Getting first 6 word after shuffle.
  $_SESSION["code"] = $captchanumber; // Initializing session variable with above generated sub-string
  $image = imagecreatefromjpeg("bj.jpg"); // Generating CAPTCHA
  $foreground = imagecolorallocate($image, 175, 199, 200); // Font Color
  imagestring($image, 5, 45, 8, $captchanumber, $foreground);
  header('Content-type: image/png');
  imagepng($image);
  </code></pre>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thank you in advance.</p>