Hello folks. I need some PHP help..obviously. I attempted to use my own logic to resolve the issue but that of course didn't work. Basically, I needed to add captcha validation which I did using "recaptcha" plugin. While it works, it's a very clumsy implementation of it, if I may say that. The code is below.

So right now, when you fill out the form correctly on the first go, including the captcha text, it submits no problem. But say you miss a field and when you get to the captcha validation, you input the right text and hit submit. As expected, once it validates the text, it brings up the "Missing Field!" error. You go back and make appropriate changes but when you hit submit this time, the captcha validation fails and you have to refresh the image and re-submit. It makes sense why this occurs, but it's extremely annoying.

I was hoping someone could be kind enough to modify the code or perhaps guide on how I can make it so that the captcha validation happens at the very end. Meaning, if there is a blank form and someone hits submit, it should say "Missing Fields!" or "Sorry. Not a valid Email!", basically going through the entire form validation process before it gets to the captcha validation.

I've been playing around with the "if" statement all day and I can't seem to find a resource online. Any help would be appreciated.

Contact Page: http://khangp.com/#contact


PHP Code:
<head>
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>

<?php
  
require_once('recaptchalib.php');
  
$privatekey "6LdedO4SAAAAAJiL3Lfa-c_GpT4W5Ij9M9ZGKen-";
  
$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
    
die ("The reCAPTCHA wasn't entered correctly.\n<br /> Go back and try it again." .
         
"\n<br />(reCAPTCHA said: " $resp->error ")");
  } else {
    
// Your code here to handle a successful verification
  
}


    
//EMAIL VALIDATION
    
function validateEmail($value){
        return 
preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'$value);
    }
    
    
//CHECK VARIABLES (EMPTY/NULL OR DEFAULT)
    
if ( isset($_POST['name']) && $_POST['name']!="Name" && isset($_POST['email']) && $_POST['email']!="Email" && isset($_POST['subjectSelect']) && $_POST['subjectSelect']!="" && isset($_POST['message']) && $_POST['message']!="Your comments..." ) {
        
        
//CHECK EMAIL    
        
if ( validateEmail($_POST['email']) ) {
            
            
            
            
////////////////////// EDIT HERE  /////////////////////////
            
            //SET HERE YOUR DESTINATION EMAIL
            //IT SHOULD BE FROM THE SAME DOMAIN WHERE SITE IS HOSTED
            
$destination="info@khangp.com";
            
            
//SET HERE YOUR EMAIL SUBJECT
            
$subject$_POST['subjectSelect'];

            
//MESSAGE DATA (HTML FORMATTED)
            
$mailMessage.="<dt><strong>Name:</strong></dt><dd>".$_POST['name']."</dd>";
            
$mailMessage.="<dt><strong>E-mail:</strong></dt><dd>".$_POST['email']."</dd>";
            
$mailMessage.="<dt><strong>Comments:</strong></dt><dd>";  
            
$mailMessage.=nl2br($_POST['message'])."</dd></dl>";
            
$mailMessage utf8_decode($mailMessage);
            
            
////////////////////// END EDIT  /////////////////////////
            
            
            //SENDER EMAIL
            
$mailFrom=$_POST['email'];
            
            
//HEADER DATA
            
$mailHeader="From:".$mailFrom."\nReply-To:".$_POST['name']."<".$mailFrom.">\n"
            
$mailHeader=$mailHeader."X-Mailer:PHP/".phpversion()."\n"
            
$mailHeader=$mailHeader."Mime-Version: 1.0\n"
            
$mailHeader=$mailHeader."Content-Type: text/html";
            
            if ( 
mail($destination,$subject,$mailMessage,$mailHeader) ) {
                echo 
'<span class="output">Message successfully sent!<br />We will contact you shortly.<br /> Thank you!</span>';
            }            
            else echo 
'<span class="outputred">Server error. Please, try again later</span>';
            
        }        
        else echo 
'<span class="outputred">Sorry. Not a valid Email!</span>';    //EMAIL VALIDATION ERROR
        
    
}
    else echo 
'<span class="outputred">Missing fields!</span>';        //VARS ERROR    
    
    
?>