validating captcha with jquery

Permalink
i made a contact us page on my website.
there is a controller method call validate()
before the form is sent i validate the captcha using jquery
$captcha = Loader::helper('validation/captcha');
   if ($captcha->check()){
      echo "true";
      setcookie("human", 1, time()+3600 , '/');
   }
   else{
      echo "false";
   }

all validates as planned and works. the problem is when i try to load that check again when the form is submitted.

the:
$captcha->check()

shows true only once for each captcha then it destroys that session of the captcha or something like that.

how do i prevent that from happening?

thanks a lot. Aryeh.

View Replies:
JohntheFish replied on at Permalink Reply
JohntheFish
That looks like php code, not JavaScript/jQuery.
aryeh replied on at Permalink Reply
JavaScript/jQuery works.
the php is only allowing me to validate the captcha once.
the jQuery is sending a post request to another page and checking if the captcha is valid if it is it returns ture, if not it returns false.
once the php gives me a true i can not do the check again.
if i do the check again i will always get false (even if i to it on the same page right after each other).

for example:
echo $captcha->check();
echo $captcha->check();


will return:
echo $captcha->check(); -> true
echo $captcha->check(); -> false
JohntheFish replied on at Permalink Reply
JohntheFish
I must be missing something that precludes the obvious:
$result = $captcha -> check();
echo $result;
echo $result;
aryeh replied on at Permalink Reply
let me try to explain more clearly:

i have a form with a captcha, while the captcha is filled out it sends a request to check if the captcha was put in correctly.
the request gets sent to a php page for validation it sends back to the form if the captcha is valid or not ( and will not allow the form to submit if the captcha is invalid. )

if the form is valid it works to submit the form.
now.. i would like to check again if the captcha was valid on the process page (because if javascript is turned off they can still submit the form).

however since i already checked on another page if the form was valid i can not check again.

i hope i explained it more clear. sorry for the trouble.
JohntheFish replied on at Permalink Reply
JohntheFish
On the first submission, store the result in $_SESSION. On the second submission, only bother to make the make the captcha check if there is nothing set in $_SESSION.

As the fail behaviour could easily be the same as the no-result yet behaviour, it could be optimised to something like:

$_SESSION['my_captcha_result'] = $captcha -> check();
if ($_SESSION['my_captcha_result']){
  // OK
}



if( $_SESSION['my_captcha_result'] || $captcha -> check()){
  // OK
}
JohntheFish replied on at Permalink Reply
JohntheFish
Thinking more, perhaps it could be further optimised to the same code in both cases:

if( $_SESSION['my_captcha_result'] || $captcha -> check()){
  // OK
  $_SESSION['my_captcha_result'] = true;
}