Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Captcha form deleting values when captcha is entered wrong

  1. #1
    Join Date
    Oct 2010
    Posts
    70
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Captcha form deleting values when captcha is entered wrong

    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


    Code:
    <?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>

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    Echo the values of $_POST['fieldname'] back into their inputs:

    PHP Code:
    <input type="text" name="myfield" value="<?php if (!empty($_POST['fieldname'])) { echo $_POST['fieldname']; } ?>" />
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Oct 2010
    Posts
    70
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    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

    Code:
    <?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
    ?>

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    mat420 (02-17-2011)

  6. #5
    Join Date
    Oct 2010
    Posts
    70
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    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 /

    Code:
    <?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>

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    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']); } ?>" />
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    mat420 (02-17-2011)

  9. #7
    Join Date
    Oct 2010
    Posts
    70
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Talking

    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

  10. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. The Following User Says Thank You to djr33 For This Useful Post:

    mat420 (02-17-2011)

  12. #9
    Join Date
    Oct 2010
    Posts
    70
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    awesome man worked.

  13. #10
    Join Date
    Oct 2010
    Posts
    70
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    tyvm ! !

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •