Link to home
Start Free TrialLog in
Avatar of altariamx2003
altariamx2003Flag for Mexico

asked on

How to Keep Data fields When Recaptcha is Invalid

I trying google recaptcha with this example: www.grupossc.com/pruebas/recaptcha/ejemplo3.php

Everything works fine but after I enter a invalid code, the fields of my form are clear

What can I do to ensure that if I enter a wrong code the fields of my form keep the information

this is the code:
<?php
require_once('recaptchalib.php');
$miFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $miFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


$captcha_publickey = "xxxxxxxxxxxxxxx";
$captcha_privatekey = "xxxxxxxxxxxxxxx";
$error_captcha=null;

if ($_POST){
   $captcha_respuesta = recaptcha_check_answer ($captcha_privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$accion = 1;
}

?>
<html>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Pagina nueva 1</title>
</head>
<body>
<form name="miforma" enctype="multipart/form-data" method="POST" action="<?php echo $miFormAction; ?>">
test field: <input type="text" name="test" size="27"><br />
<?php echo recaptcha_get_html($captcha_publickey, $error_captcha); ?><br />
<input type="submit" value="Enviar" name="B1">
</form>
</body>

</html>
<?php


if ($accion == 1)
{
  if ($captcha_respuesta->is_valid) 
  {echo "<script type=\"text/javascript\">alert(\"Captcha code is correct\");</script>"; }
 else
 {echo "<script type=\"text/javascript\">alert(\"Captcha code is not correct\");</script>";}	  
}
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Uhh... why would you want to keep information when the only human-test element fails?

But that aside, the big-picture concept would be something like this:

1. Client submits form
2. Server copies form data to PHP session
3. Server validates form data
4. Whether or not server validation works, server reloads input value= attributes with data from session.
5. If validation is OK, server processes form data
6. if validation is not OK, server sends form data back to client with the prepopulated form.
ASKER CERTIFIED SOLUTION
Avatar of Michael
Michael

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of altariamx2003

ASKER

Hi Ray

The reason is I want that when the user fill ok all the data fields but he put a wrong captcha  he only need to re-write just the captcha code and not all data fields in my form
This is the solution