jeudi 1 septembre 2016

php if(!$variable) not working

Please forgive me if this is a dumb question, as I am new to php. I did some c++ years ago and are dabbling in c# with Unity.

I am trying to get a working html / js / php contact form working with reCaptcha. The form works well if the captcha is checked, and the 'success' message is displayed. If the captch is not checked, I want to display a message and not send the form email. The code snippet below works insofar as the email is NOT sent if the captcha is not ticked, but it does not display the message. Can anyone help?

$okMessage = 'Contact form successfully submitted. Thank you, we will get back to you soon!';
$captchaMessage = 'There was an error while submitting the form. Please check the captcha box.';
$spamMessage = 'There was an error while submitting the form. Spamers not welcome.';
$captcha = $_POST['g-recaptcha-response'];

try
{
    //Sanitise Inputs
    $emailText = "You have new message from contact form\n=============================\n";
    $emailText .= "First Name: " . @trim(stripslashes($_POST['name'])). "\n";
    $emailText .= "Last Name: " . @trim(stripslashes($_POST['surname'])). "\n";
    $emailText .= "Company: " . @trim(stripslashes($_POST['company'])). "\n";
    $emailText .= "Email: " . @trim(stripslashes($_POST['email'])). "\n";
    $emailText .= "Message: " . @trim(stripslashes($_POST['message'])). "\n";

    if(!$captcha){
        $responseArray = array('type' => 'danger', 'message' => $captchaMessage);
          exit;
        }
        $secretKey = "secret_key";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("http://ift.tt/1vh5G4o".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          $responseArray = array('type' => 'danger', 'message' => $spamMessage);
        } else {
            mail($sendTo, $subject, $emailText, "From: " . $from);
            $responseArray = array('type' => 'success', 'message' => $okMessage);
        }


}

Aucun commentaire:

Enregistrer un commentaire