php if(!$variable) not working



如果这是一个愚蠢的问题,请原谅我,因为我是php的新手。我几年前做过一些c++,现在在Unity中尝试c#。

我试图得到一个工作的html/js/php联系形式与reCaptcha工作。如果验证码被选中,表单将正常工作,并显示"成功"消息。如果没有检查捕获,我希望显示一条消息,而不是发送表单电子邮件。如果没有勾选验证码,则不发送电子邮件,下面的代码片段可以工作,但它不显示消息。有人能帮忙吗?

$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 formn=============================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("https://www.google.com/recaptcha/api/siteverify?secret=".$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);
    }
}

您的exit停止脚本。因此不能显示消息

if(!$captcha){
        $responseArray = array('type' => 'danger', 'message' => $captchaMessage);
}
else
{
        $secretKey = "secret_key";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$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);
        }
}
// Here the script continue

最新更新