Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Keep Data fields When Recaptcha is Invalid

Posted on 2014-04-01
4
Medium Priority
?
1,031 Views
Last Modified: 2014-04-02
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

0
Comment
Question by:altariamx2003
  • 2
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39971081
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.
0
 
LVL 2

Accepted Solution

by:
Michael earned 2000 total points
ID: 39971590
Give this a try and see if it works for you.

<?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; ?>">
First Name: <input type="text" name="first_name" size="27" value="<?php if(isset($_POST['first_name'])) { echo htmlentities ($_POST['first_name']); }?>"><br />
Last Name: <input type="text" name="last_name" size="27" value="<?php if(isset($_POST['last_name'])) { echo htmlentities ($_POST['last_name']); }?>"><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

0
 

Author Comment

by:altariamx2003
ID: 39973338
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
0
 

Author Closing Comment

by:altariamx2003
ID: 39973340
This is the solution
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question