Solved

How to Keep Data fields When Recaptcha is Invalid

Posted on 2014-04-01
4
844 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 108

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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now