Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2011, 06:10 PM   PM User | #1
moss2076
Regular Coder

 
Join Date: Oct 2005
Posts: 332
Thanks: 42
Thanked 2 Times in 2 Posts
moss2076 is an unknown quantity at this point
Question about ReCaptcha - display error on form instead of blank page

Hello,

I have a php "contact.php" form. The code incorporates ReCaptcha php code for a bit of extra security.

If the submit button is pressed without entering the captcha code the form redirects to a blank page and a message is displayed - "The reCAPTCHA
wasn't entered correctly. Go back and try it again.(reCAPTCHA said:
incorrect-captcha-sol)".

I would much prefer the message to appear on my contact.php form directly below the submit button after it is pressed, instead of on a seperate blank page..

Can this be achieved?
PHP Code:
 <?php  
    $errors 
null;  
if (isset(
$_POST['submit']))  
{   
    
$name $_POST['name']; 
    
$emailSubject $_POST['emailSubject']; 
    
$email $_POST['email'];  
    
$message $_POST['message'];  
     
/*Check required fields have been filled out*/     
$mandate=array($name$email); // Mandatory fields  
if(empty($name))  
  
$errors['name']="Please Enter Your Name!";  
  if (empty(
$email))  
   
$errors['email']= 'Please Enter Your Email Address!';  
 if(!
preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$email))  
  
$errors['email']='Please check your email address!';  
/*End of Check required fields have been filled out*/ 
     
/*Re-captcha code starts here - linked to recaptchalib.php*/ 
require_once('recaptchalib.php'); 
$privatekey "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$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 ")"); 

/*Re-captcha code ends here*/ 
     

/*Email Address Validator Start here - linked to EmailAddressValidator.php*/  
    
include('EmailAddressValidator.php');  
    
$validator = new EmailAddressValidator;  
    if (
$validator->check_email_address($email))  
/*Email Address Validator End here*/ 

// code to send email     
   
if(count($errors)==0){ 
  
        
$to 'me@me.com';  
        
$subject 'Email query from xxxx';    
        
$message "Email sent from: $email \n $name has sent you a message:- \n $message"
        
$headers 'From: me@me.com'"\r\n" 
        
'Reply-To: queries@me.com'"\r\n" 
        
'X-Mailer: PHP/' phpversion(); 
        
mail($to$subject$message$headers) or exit('mail FAIL');  
        echo 
'<p>Thank you for emailing me! Your Message has been sent.</p>';  
    }  
    else  
    {  
        echo 
'<p>Please re-check your form entries.</P>';  
    }  
}   

?>  

<?php  
if(!isset($_POST['submit']) || count($errors)){ 
?> 

<form id="tomsform" method="POST" action="<?php echo JRoute::_'index.php' );?>"> 

<p>First Name</p> 
  <input type="text" name="name" size="19" value="<?php echo (isset($name)) ? $name '';?>" class="textbox"> 
   <?php 
  
if(isset($errors['name'])) echo $errors['name']; 
  
?> 
   <br> 
   <p>Email Subject</p> 
  <input type="text" name="emailSubject" size="19" value="<?php echo (isset($emailSubject)) ? $emailSubject '';?>" class="textbox"> 
   <?php 
  
if(isset($errors['emailSubject'])) echo $errors['emailSubject']; 
  
?> 
   <br>
   <p>Email Address</p> 
   <input type="text" name="email" size="19" value="<?php echo (isset($email)) ? $email '';?>" class="textbox"> 
  <?php 
  
if(isset($errors['email'])) echo $errors['email']; 
  
?> 
   <br> 
   <p>Message</p> 
   <textarea rows="9" name="message" cols="30" class="textbox"><?php echo (isset($message)) ? $message'';?></textarea> 
   <br> 
   <br> 
 <div id="x"> 
 <?php 
   
require_once('recaptchalib.php'); 
$publickey "xxxxxxxxxxxxxxxxxxxxxxxxx"// you got this from the signup page  
echo recaptcha_get_html($publickey); 

if(isset(
$errors['captcha'])) echo $errors['captcha'];  
?>  
 </div> 
<input type="submit" value="Submit" name="submit" class="go"> 
</form> 
<?php 

?>
Many thanks
moss2076 is offline   Reply With Quote
Old 11-20-2011, 06:30 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
You could use AJAX (combination of PHP and javascripting).

Otherwise, when you return back to your form, the text boxes
will be empty. So, you'll have to use PHP SESSION variables to
remember the values.

Sorry I don't have an actual script to show ... you might be
able to find a good example via Google ... like this:
https://www.google.com/#sclient=psy-...w=1366&bih=607
mlseim is offline   Reply With Quote
Old 11-20-2011, 06:38 PM   PM User | #3
moss2076
Regular Coder

 
Join Date: Oct 2005
Posts: 332
Thanks: 42
Thanked 2 Times in 2 Posts
moss2076 is an unknown quantity at this point
I don't want to leave the form if the recaptcha is empty or incorrect. I want the message to appear underneath the recaptcha box on the form.

I'm sure I have seen it done
moss2076 is offline   Reply With Quote
Old 11-20-2011, 08:29 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
That's what AJAX is ... look at those examples.
They also have demos to look at.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:05 AM.


Advertisement
Log in to turn off these ads.