php if(!$ variable)无效

时间:2016-09-01 09:17:57

标签: php if-statement

如果这是一个愚蠢的问题,请原谅我,因为我是php的新手。几年前我做了一些c ++,并且正在和Unity一起讨论c#。

我正在尝试使用reCaptcha工作的html / js / php联系表单。如果选中验证码,那么表格效果很好,并且成功'消息显示。如果未选中captch,我想显示一条消息,而不是发送表单电子邮件。如果未勾选验证码,则不会发送电子邮件,但下面的代码片段不起作用,但它不显示该消息。有人可以帮忙吗?

$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("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);
    }
}

1 个答案:

答案 0 :(得分:1)

您的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