title: How to integrate (validate) recapcha in submit page
body: <p>I'm using <a href="http://tutorialzine.com/2010/06/simple-ajax-commenting-system/" rel="nofollow">simple ajax commenting system</a> and i dont now how to integrate recaptcha in submit page! </p>
  
  <p><strong>AJAX COMMENT.PHP</strong></p>
  
  <pre><code>&lt;?php
  
  // Error reporting:
  error_reporting(E_ALL^E_NOTICE);
  
  include "connect.php";
  include "comment.class.php";
  
  
  /*
  /   Select all the comments and populate the $comments array with objects
  */
  $id = empty($_GET['id'])?0:$_GET['id'];
  $comments = array();
  $result = mysql_query("SELECT * FROM comments where url='$id' ORDER BY id desc");
  
  while($row = mysql_fetch_assoc($result))
  {
      $comments[] = new Comment($row);
  }
  
  ?&gt;
  
  
  
  &lt;table width="800" border="0" class="komentar" cellpadding="0" cellspacing="0"&gt;
  &lt;tr&gt;
  &lt;td width="480"&gt;
  
  &lt;div id="addCommentContainer"  style="width:500px; overflow-x:hidden;"&gt;
  &lt;/div&gt;
  
  &lt;div id="main" style="width:500px; overflow-x:hidden;"&gt;
  
  &lt;?php
  
  /*
  /   Output the comments one by one:
  */
  
  foreach($comments as $c){
      echo $c-&gt;markup();
  }
  
  ?&gt;
  
  &lt;/div&gt;
  
  
  
  &lt;/td&gt;
  
  
  
  &lt;td valign="top" width="320"&gt;
      &lt;table width="300" border="0"  cellpadding="0" cellspacing="0"&gt;
      &lt;tr&gt;
      &lt;td height="16"  background="commentfg.png" style="background-repeat:no-repeat;"&gt;               
  
      &lt;/td&gt;
      &lt;/tr&gt;  
  
      &lt;tr&gt;
      &lt;td height="20"  background="commentfs.png"&gt;                
      &lt;form id="addCommentForm" method="post" action=""&gt;
          &lt;div style="margin-left:15px; color:white;"&gt;
  
              &lt;label for="name"&gt;Vase ime:&amp;nbsp&amp;nbsp&lt;/label&gt;&lt;br&gt;
              &lt;input type="text" name="name" id="name" /&gt;
  
           &lt;p&gt;&lt;label for="email"&gt;Email: (bice sakriven)&amp;nbsp&amp;nbsp&lt;/label&gt;&lt;br&gt;
              &lt;input type="text" name="email" id="email" /&gt;&lt;/p&gt;
  
  
              &lt;input type="hidden" name="url" id="url" value="&lt;?php $id = empty($_GET['id'])?0:$_GET['id']; echo $id;?&gt;"/&gt;
  
              &lt;label for="body"&gt;Unesite komentar:&amp;nbsp&amp;nbsp&lt;/label&gt;&lt;br&gt;
              &lt;textarea name="body" id="body" cols="30" rows="5" onKeyDown="limitText(this.form.body,this.form.countdown,200);" 
  onKeyUp="limitText(this.form.body,this.form.countdown,200);"&gt;&lt;/textarea&gt;&lt;br&gt;&lt;br&gt;
              Broj preostalih znakova:(max 200):&lt;input readonly type="text" name="countdown" size="3" value="200"&gt; 
  &lt;br&gt;
    &lt;script type="text/javascript"
       src="http://www.google.com/recaptcha/api/challenge?k=6LeQc8MSAAAAAMGe16dslbxCRu3Frs_265JhH88j"&gt;
    &lt;/script&gt;
    &lt;noscript&gt;
       &lt;iframe src="http://www.google.com/recaptcha/api/noscript?k=6LeQc8MSAAAAAMGe16dslbxCRu3Frs_265JhH88j"
           height="300" width="500" frameborder="0"&gt;&lt;/iframe&gt;&lt;br&gt;
       &lt;textarea name="recaptcha_challenge_field" rows="3" cols="40"&gt;
       &lt;/textarea&gt;
       &lt;input type="hidden" name="recaptcha_response_field"
           value="manual_challenge"&gt;
    &lt;/noscript&gt;
  &lt;br&gt;
  
              &lt;input type="submit" id="submit" value="Potvrdi" /&gt;
  
          &lt;/div&gt;
      &lt;/form&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
      &lt;td height="16"  background="commentfd.png" style="background-repeat:no-repeat;"&gt;               
  
      &lt;/td&gt;
      &lt;/tr&gt; 
      &lt;/table&gt;    
  
  
  
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p><strong>COMMENT.CLASS.php</strong> </p>
  
  <pre><code>&lt;?php
  class Comment
  {
      private $data = array();
  
      public function __construct($row)
      {
          /*
          /   The constructor
          */
  
          $this-&gt;data = $row;
      }
  
      public function markup()
      {
          /*
          /   This method outputs the XHTML markup of the comment
          */
  
          // Setting up an alias, so we don't have to write $this-&gt;data every time:
          $d = &amp;$this-&gt;data;
  
          $link_open = '';
          $link_close = '';
  
  
  
          // Converting the time to a UNIX timestamp:
          $d['dt'] = strtotime($d['dt']);
  
          // Needed for the default gravatar image:
          $url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.png';
  
          return '
             &lt;div  style="width:500px; overflow-x:hidden;"&gt;
              &lt;table width="500" &gt;
              &lt;tr&gt;
              &lt;td valign="top" width="100" height="120" background="avatar.png" style="background-repeat:no-repeat;" align="center"&gt;
  
                  &lt;div class="avatar" style="width:100px; overflow-x:hidden; margin-top:10px; "&gt;
                      '.$link_open.'
                      &lt;img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&amp;amp;default='.urlencode($url).'" /&gt;
                      '.$link_close.'
                  &lt;/div&gt;
                  &lt;div style="width:80px; height:40px; overflow:hidden;"&gt;
                  &lt;font color="#47a5db"&gt;'.$link_open.$d['name'].$link_close.'&lt;/font&gt;
                  &lt;/div&gt;
  
              &lt;/td&gt;   
              &lt;td width="400" valign="top"&gt;
                  &lt;table width="400" border="0"  cellpadding="0" cellspacing="0"&gt;
                  &lt;tr&gt;
                  &lt;td height="20" valign="bottom" background="commentg.png" style="background-repeat:no-repeat;"&gt;
                  &lt;div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'" style="margin-left:10px;"&gt;
                  &lt;font color="silver" size="-1"&gt;'.date('d M Y',$d['dt']).'&lt;/font&gt;
                  &lt;/div&gt;              
                  &lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                  &lt;td background="comments.png" style="background-repeat:repeat-y;"&gt;
                  &lt;div style="width:350px; overflow-x:hidden; margin-top:0px; margin-left:10px;"&gt;
                  &lt;p&gt;&lt;font color="white"&gt;&lt;b&gt;'.$d['body'].'&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
                  &lt;/div&gt;
                  &lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                  &lt;td height="20"  background="commentd.png" style="background-repeat:no-repeat;"&gt;                
  
                  &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/table&gt;
  
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;/table&gt;
            &lt;/div&gt;    
          ';
      }
  
      public static function validate(&amp;$arr)
      {
          /*
          /   This method is used to validate the data sent via AJAX.
          /
          /   It return true/false depending on whether the data is valid, and populates
          /   the $arr array passed as a paremter (notice the ampersand above) with
          /   either the valid input data, or the error messages.
          */
  
          $errors = array();
          $data   = array();
  
          // Using the filter_input function introduced in PHP 5.2.0
  
          if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
          {
              $errors['email'] = 'Unesite validan Email!';
          }
  
          if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_CALLBACK,array('options'=&gt;'Comment::validate_text'))))
          {
              // If the URL field was not populated with a valid URL,
              // act as if no URL was entered at all:
  
              $errors['url'] = 'Please enter a url.';
          }
  
          // Using the filter with a custom callback function:
  
          if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=&gt;'Comment::validate_text'))))
          {
              $errors['body'] = 'Niste uneli komentar!';
          }
  
          if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=&gt;'Comment::validate_text'))))
          {
              $errors['name'] = 'Niste uneli ime!';
          }
  
          if(!empty($errors)){
  
              // If there are errors, copy the $errors array to $arr:
  
              $arr = $errors;
              return false;
          }
  
          // If the data is valid, sanitize all the data and copy it to $arr:
  
          foreach($data as $k=&gt;$v){
              $arr[$k] = mysql_real_escape_string($v);
          }
  
          // Ensure that the email is lower case:
  
          $arr['email'] = strtolower(trim($arr['email']));
  
          return true;
  
      }
  
      private static function validate_text($str)
      {
          /*
          /   This method is used internally as a FILTER_CALLBACK
          */
  
          if(mb_strlen($str,'utf8')&lt;1)
              return false;
  
          // Encode all html special characters (&lt;, &gt;, ", &amp; .. etc) and convert
          // the new line characters to &lt;br&gt; tags:
  
          $str = nl2br(htmlspecialchars($str));
  
          // Remove the new line characters that are left
          $str = str_replace(array(chr(10),chr(13)),'',$str);
  
          return $str;
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>SUBMIT.PHP</strong></p>
  
  <pre><code>&lt;?php
  
  // Error reporting:
  error_reporting(E_ALL^E_NOTICE);
  
  include "connect.php";
  include "comment.class.php";
  
  /*
  /   This array is going to be populated with either
  /   the data that was sent to the script, or the
  /   error messages.
  /*/
  
  $arr = array();
  $validates = Comment::validate($arr);
  
  if($validates)
  {
      /* Everything is OK, insert to database: */
  
      mysql_query("   INSERT INTO comments(name,url,email,body)
                      VALUES (
                          '".$arr['name']."',
                          '".$arr['url']."',
                          '".$arr['email']."',
                          '".$arr['body']."'
                      )");
  
      $arr['dt'] = date('r',time());
      $arr['id'] = mysql_insert_id();
  
      /*
      /   The data in $arr is escaped for the mysql query,
      /   but we need the unescaped variables, so we apply,
      /   stripslashes to all the elements in the array:
      /*/
  
      $arr = array_map('stripslashes',$arr);
  
      $insertedComment = new Comment($arr);
  
      /* Outputting the markup of the just-inserted comment: */
  
      echo json_encode(array('status'=&gt;1,'html'=&gt;$insertedComment-&gt;markup()));
  
  }
  else
  {
      /* Outputtng the error messages */
      echo '{"status":0,"errors":'.json_encode($arr).'}';
  }
  
  ?&gt;
  </code></pre>