Solved

How to Keep Data fields When Recaptcha is Invalid

Posted on 2014-04-01
4
960 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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