Log in

View Full Version : Captcha form deleting values when captcha is entered wrong



mat420
02-15-2011, 07:57 PM
when someone entered the wrong captcha it works perfect but it deletes everything they typed, is there any easy way to make it save what they typed in multiple fields? (its gonna by like name, email, message, company)

or is it easier to just find another captcha ?

its Boutells captcha form Boutell.com
thanks a lot.
it might not be up long but heres the example
http://aciddr0p.net/drtest/captcha/demo.php





<?php
// Always at the VERY TOP of the page.
// The opening php tag above has to be the
// VERY FIRST thing in your page, NO blank lines,
// no NOTHING EVER, or it will NOT work. Yes, really!
require 'captcha.php';

// Now $captchaimg and $captchawav are set and we can introduce
// those links wherever we like in the page. We can also
// access the captcha code as $_SESSION['captchacode']
// and verify what the user enters in our form, as shown
// below.

// Where to send the messages users enter in the contact form
// (change to your address if you really use this)
$myaddress = 'Mssss@GMAIL.COM';
?>

<?php
if ($_POST['send']) {
$errors = array();
if ($_POST['captcha'] != $_SESSION['captchacode']) {
$errors[] = "You did not enter the letters shown in the image.";
}
if (!count($errors)) {
// IMPORTANT: If you don't call this the
// user will keep getting the SAME code!
captchaDone();
$message = $_POST['message'];
mail($myaddress, 'Contact Form Submission', $message);
// Notice we can shift in and out of "HTML mode"
// to display some HTML only when the
// user passes the test
?>
<html>
<head>
<title>Message Sent</title>
</head>
<body>
<h1>Message Sent</h1>
Thank you for using our handy contact form.
<p>
<!-- Generate a link back to ourselves -->
<a href="<?php echo $SERVER['SCRIPT_URL']?>">Contact Us Again</a>
</body>
</html>
<?php
// Exit now to prevent the original form from
// appearing again
exit(0);
}
}
?>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h1>Contact Us</h1>
<?php
if (isset($errors)) {
foreach ($errors as $error) {
echo("<p>$error<p>\n");
}
}
?>
<p>
<form method="POST" action="<?php echo $SERVER['SCRIPT_URL']?>">
<p>
<b>Verification Code</b>
<p>
To prove you are a human being, you must enter the lowercase letters shown
below in the field on the right. Thank you for your understanding!
<p>
<img style="vertical-align: middle" src="<?php echo captchaImgUrl()?>">&nbsp;&nbsp;<input name="captcha" size="8"/>
<a href="<?php echo captchaWavUrl()?>">Listen To This</a>
<p>
Please enter your message in the text field below. Then click
"Send Your Message."
<p>
<textarea name="message" rows="10" cols="60">
</textarea>
<p>
<input type="submit" name="send" value="Send Your Message"/>
</form>
</body>
</html>

djr33
02-15-2011, 10:10 PM
Echo the values of $_POST['fieldname'] back into their inputs:


<input type="text" name="myfield" value="<?php if (!empty($_POST['fieldname'])) { echo $_POST['fieldname']; } ?>" />

mat420
02-16-2011, 02:41 AM
u know i tried that like, exact code

didnt know where to put it though
i tried a few variations of putting it her, but couldnt figure it out for the life of me




<?php
if ($_POST['send']) {
$errors = array();
if ($_POST['captcha'] != $_SESSION['captchacode']) {
$errors[] = "You did not enter the letters shown in the image.";
<input type="text" name="myfield" value="<?php if (!empty($_POST['fieldname'])) { echo $_POST['fieldname']; } ?>" />
}
if (!count($errors)) {
// IMPORTANT: If you don't call this the
// user will keep getting the SAME code!
captchaDone();
$message = $_POST['message'];
mail($myaddress, 'Contact Form Submission', $message);
// Notice we can shift in and out of "HTML mode"
// to display some HTML only when the
// user passes the test
?>

djr33
02-16-2011, 02:45 AM
Wherever your original input is located, just add the value="..." part to that. And it should work, nothing more than that.

Also, to be safe it's good to use:
echo htmlentities($_POST['myfield']);

htmlentities escapes things like " and < so that if the field contains HTML (or something that looks like HTML to the browser) it will ignore it and make it into &quot; and &lt; etc.

mat420
02-16-2011, 07:11 AM
where would i put echo htmlentities($_POST['myfield']); please?

thanks so much i got it to echo back into the text field correct

ive now put, hours into this stupid thing (i need to just learn this stuff) and i cant figure out how to enter more than one text field to submit if the captcha is right
as soon as i add a third thing to send me ($message in this case), it either ignores the third or doesnt send at all (i forget which), i tried everything i can think of.

thanks so much, heres the full code :o/



<?php
// Always at the VERY TOP of the page.
// The opening php tag above has to be the
// VERY FIRST thing in your page, NO blank lines,
// no NOTHING EVER, or it will NOT work. Yes, really!
require 'captcha.php';

// Now $captchaimg and $captchawav are set and we can introduce
// those links wherever we like in the page. We can also
// access the captcha code as $_SESSION['captchacode']
// and verify what the user enters in our form, as shown
// below.

// Where to send the messages users enter in the contact form
// (change to your address if you really use this)
$myaddress = 'mathew420@gmail.com';
?>

<?php
if ($_POST['send']) {
$errors = array();
if ($_POST['captcha'] != $_SESSION['captchacode']) {
$errors[] = "You did not enter the letters shown in the image.";

}
if (!count($errors)) {
// IMPORTANT: If you don't call this the
// user will keep getting the SAME code!
captchaDone();
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
mail($myaddress, 'DRPCREPAIR Customer', $name_field, $email_field, $message);
// Notice we can shift in and out of "HTML mode"
// to display some HTML only when the
// user passes the test
?>
<html>
<head>
<title>Message Sent</title>
</head>
<body>
<h1>Message Sent</h1>
Thank you for using our handy contact form.
<p>
<!-- Generate a link back to ourselves -->
<a href="<?php echo $SERVER['SCRIPT_URL']?>">Contact Us Again</a>
</body>
</html>
<?php
// Exit now to prevent the original form from
// appearing again
exit(0);
}
}
?>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h1>Contact Us</h1>
<?php
if (isset($errors)) {
foreach ($errors as $error) {
echo("<p>$error<p>\n");
}
}
?>
<p>
<form method="POST" action="<?php echo $SERVER['SCRIPT_URL']?>">
<p>
<b>Verification Code</b>
<p>
To prove you are a human being, you must enter the lowercase letters shown
below in the field on the right. Thank you for your understanding!
<p>
<img style="vertical-align: middle" src="<?php echo captchaImgUrl()?>">&nbsp;&nbsp;<input name="captcha" size="8"/>
<a href="<?php echo captchaWavUrl()?>">Listen To This</a>
<p>
Please enter your message in the text field below. Then click
"Send Your Message."
<p>
<input type="text" name="name" value="<?php if (!empty($_POST['name'])) { echo $_POST['name']; } ?>" />
</textarea><p>
<input type="text" name="email" value="<?php if (!empty($_POST['email'])) { echo $_POST['email']; } ?>" />
</textarea><p>
<input type="text" name="message" value="<?php if (!empty($_POST['message'])) { echo $_POST['message']; } ?>" />
</textarea>

<p>
<input type="submit" name="send" value="Send Your Message"/>
</form>
</body>
</html>

djr33
02-16-2011, 07:00 PM
Sorry I wasn't clear. You will combine the htmlentities() code with the original. So:
<input type="text" name="name" value="<?php if (!empty($_POST['name'])) { echo htmlentities($_POST['name']); } ?>" />

mat420
02-17-2011, 04:55 AM
hey thanks is there an easy method like this for textarea or whatever its called? i need a msgbox with like 3 rows and 30 colums or something.

thanks x million

figured out my other issue and...aside from that one ^ of course ran straight into another. giving it my 5 hours of trying before i post for help ;/ ha :(

djr33
02-17-2011, 01:55 PM
There's a similar method for any type of input.
A textarea isn't like the other inputs because it's a different tag. Instead of a value input, the "value" is just placed between the tags. So modify the code above like this:
<textarea name="name"><?php if (!empty($_POST['name'])) { echo htmlentities($_POST['name']); } ?></textarea>

Note that this method works for most types of input, perhaps with some manipulation (such as for a checkbox).
However, for a select menu (maybe radio buttons as well) it becomes more complicated because you need to use a loop to determine which value was selected.

mat420
02-17-2011, 08:54 PM
awesome man worked.

mat420
02-17-2011, 09:12 PM
tyvm ! !

mat420
02-17-2011, 09:13 PM
drj33, btw not that i can help with code. but if u ever need some like, "man hours" toward ur site (just adding some content or something) let me know. obviouslyi dont need to access ur server or anything to do this. just would like to show my appreciation. thank you.

djr33
02-18-2011, 01:34 AM
I'm glad it worked out. Thanks for the offer, but at least at the moment my projects aren't really moving (busy with school). If you happen to speak a language other than English and are interested in languages, then I've got a project that might interest you and I'd certainly like the help.
But really I'm just happy to help, so if you want to return the favor just keep visiting the site and help others if you see a question to which you know the answer. (This actually is, in my opinion, the best way to learn more about web design.)

mat420
02-18-2011, 05:42 AM
nah just english for me at the moment sorry :/
yeah i plan to start trying to give back to the community as soon as i finally finish this site.
hopefully there will actually be things i can answer/find the answer to.
im pretty good with computers, just not any programming languages :/

djr33
02-18-2011, 05:51 AM
No problem and welcome to the site.