Jump to content

[SOLVED] Need Help Getting reCAPTCHA Error To Display On Form Page!


designer76

Recommended Posts

I have my form-to-email all set up and working (and looking) just how I want it to. The only thing left is to get the reCAPTCHA error (like if they type in the wrong words) to display somewhere on my same form page instead of it taking people to a new page just to display the problem.

 

I have tried everything I can think of and nothing is working, the problem is that the actual script is in a separate file than my form that has the reCAPTCHA view, so it is kind of tricky for me to get this thing working. I have posted the code below that controls the reCAPTCHA error if this helps.

 

Also, it seems as if the reCAPTCHA script only works works at all if those two pieces of code are separated, when they are in the same file I either get a white screen, or some of my HTML view is being cut off under where I place the larger script. Thanks in advance to anyone who can help me.

 

 

Actual code in my form page that displays the reCAPTCHA

<?php
require_once('recaptchalib.php');
$publickey = "fhfghgfhgfhgfhgfh8gfh8gf8dhgf89fgfdggf"; // you got this from the signup page
echo recaptcha_get_html($publickey); ?>

 

reCAPTCHA code in my Form-to-E-mail Script

<?php

require_once('recaptchalib.php');

$privatekey = "fdgfdgfdgfdgfgdgfdgfggfgd54465464654365254";

$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
?>

Link to comment
Share on other sites

well, since it is using php, the only way for the script to validate the form entry is to refresh the page/go to a new page. This is because PHP is parsed server side. If you want it to be more "dynamic" I think you will have to use some combination of javascript or AJAX.

Link to comment
Share on other sites

well, since it is using php, the only way for the script to validate the form entry is to refresh the page/go to a new page. This is because PHP is parsed server side. If you want it to be more "dynamic" I think you will have to use some combination of javascript or AJAX.

So there is no way that it can refresh the page and show me the reCAPTCHA error there? Or if I use the header function to kick me back to the form page, is there a way that I can populate the reCAPTCHA error there that way?

Link to comment
Share on other sites

I suppose you could just put the recaptcha check in the same page as the form, and have the form submit to itself. But since you are already refreshing the page, I don't how making it go to the same apge is better

The problem is that the reCAPTCHA check has to be in a separate file. I have tried combining those two pieces of code in the same file and I end up with a white screen, or my HTML gets cut off wherever I place the check code. Is there a way I can make this work that I am not doing?

Link to comment
Share on other sites

Ill show my code in case it helps.

 

first, i include these 2 lines at the top of the page, after i include the header

// Needed here for recaptcha
include_once('./includes/recaptcha/recaptchalib.php');
$private_key = '6LcSGaasdaswYAAAewr3312Y5_VPFfSvyetkopLdulIJdJTDI';

 

Then a little down after some HTML,

I use this php code when my contact form is submitted.


<?php

if(isset($_POST['submit'])) {

	$name		= $_POST['name'];
	$email		= $_POST['email'];
	$comments	= nl2br($_POST['comments']);

	// for recaptcha
                include_once('./includes/recaptcha/recaptchalib.php');

	$resp = recaptcha_check_answer($private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

	$error = null;
	$error .= ( empty($name) )                   ? '<li>You must enter a name.</li>' : null;
	$error .= ( empty($comments) )             ? '<li>You must enter some comments or questions.</li>' : null;
	$error .= ( !$resp->is_valid )                  ? '<li>The captcha code entered is incorrect('.$resp->error.')</li>' : null;
	$error .= ( !validate_email($email) )	        ? '<li>Email is blank or has an invalid format.</li>' : null;



	if( ! $error ) {

		// email form
		contact_form( $name, $email, $comments );

		echo '<h2>Thank You.</h2>';
		echo 'Your e-mail has been sent, and a Site2Street representative will contact you shortly.</div></div>';
		include_once('./includes/footer.php');
		exit();
	} else {
		echo '<div class="error">'. $error .'</div>';		
	}


}

?>

 

works perfectly for me

Link to comment
Share on other sites

Ill show my code in case it helps.

 

first, i include these 2 lines at the top of the page, after i include the header

// Needed here for recaptcha
include_once('./includes/recaptcha/recaptchalib.php');
$private_key = '6LcSGaasdaswYAAAewr3312Y5_VPFfSvyetkopLdulIJdJTDI';

 

Then a little down after some HTML,

I use this php code when my contact form is submitted.


<?php

if(isset($_POST['submit'])) {

	$name		= $_POST['name'];
	$email		= $_POST['email'];
	$comments	= nl2br($_POST['comments']);

	// for recaptcha
                include_once('./includes/recaptcha/recaptchalib.php');

	$resp = recaptcha_check_answer($private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

	$error = null;
	$error .= ( empty($name) )                   ? '<li>You must enter a name.</li>' : null;
	$error .= ( empty($comments) )             ? '<li>You must enter some comments or questions.</li>' : null;
	$error .= ( !$resp->is_valid )                  ? '<li>The captcha code entered is incorrect('.$resp->error.')</li>' : null;
	$error .= ( !validate_email($email) )	        ? '<li>Email is blank or has an invalid format.</li>' : null;



	if( ! $error ) {

		// email form
		contact_form( $name, $email, $comments );

		echo '<h2>Thank You.</h2>';
		echo 'Your e-mail has been sent, and a Site2Street representative will contact you shortly.</div></div>';
		include_once('./includes/footer.php');
		exit();
	} else {
		echo '<div class="error">'. $error .'</div>';		
	}


}

?>

 

works perfectly for me

 

Thanks man, I really really appreciate your help, but I have just figured out another way to do it, although after looking at the way your code looks I man modify a few things. Thanks again for your reply your post is still very helpful!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.