Jump to content

PHP form with Captcha - amnyone used this?


pixelchicken

Recommended Posts

Hi all,

 

I have found a thread that relates to my query but I'm a novice so it doesn't make any sense..

 

Basically, I have just implemented captcha on my site (php code below) and it's all working yey! All I need to do, is to alert the user if they enter an incorrect code, and *stay on the same page*. I have the alert working fine in Javascript, but it then takes the user to a blank page. I want to alert them (*you've entered a wrong code*) and then stay on the same page.

 

Simple enough right?

 

Any ideas grately appriciated!

 

Code:

 

<?php

require_once('captcha/recaptchalib.php');

$privatekey = "6Lcil78SAAAAALYbGUXf2ophiajdWJuPho7A63La";

$resp = recaptcha_check_answer ($privatekey,

$_SERVER["REMOTE_ADDR"],

$_POST["recaptcha_challenge_field"],

$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly

 

?>

<script language="javascript">

alert("Verification words do not match image. Please try again.");

</script>

<?

 

// **** NEED CODE HERE TO STAY ON SAME PAGE ****

 

 

header( "Location: contact.php" );

 

} else {

 

// Code here to handle a successful verification

 

$to = "[email protected]";

$subject = "Contact / Booking Form from the Trevalia Website";

$name = $_REQUEST['name'];

$email_field = $_POST['email'];

$phone = $_POST['phone'];

$comments = $_POST['comments'];

 

*******";

$body = "From: $name \n\n E-Mail: $email_field\n\n Phone Number: $phone\n\n Message:\n $comments";

header( "Location: thankyou_contact.html" );

mail($to, $subject, $body, $headers);

}

?>

Link to comment
https://forums.phpfreaks.com/topic/222765-php-form-with-captcha-amnyone-used-this/
Share on other sites

So you are wanting the form to be submitted using AJAX so there is no refresh involved? Seems to me you have the line

header( "Location: contact.php" );

 

which is refreshing the page aswell.

 

Like PHPTOM said, have you tried removing

header( "Location: contact.php" );

?

 

From what I know, alert itself won't redirect a page. the only reason it redirects the page is because it executes redirection command, which is 

header( "Location: contact.php" );

in this case.

 

FYI: I never use that particular library for CAPTCHA. However, I used CAPTCHA with other library before.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.