Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Keep Data fields When Recaptcha is Invalid

Posted on 2014-04-01
4
Medium Priority
?
1,007 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 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