php reCaptcha在提交后不会返回确认

时间:2017-01-16 14:28:11

标签: php html forms

我的网站www.boakustikk.no上有reCaptcha的联系表格 几乎一切都在运作。只有在正确执行reCaptcha时,才会发送电子邮件。但在两种情况下,返回确认只是一个空白的空页。似乎form.php中第31行到第41行的函数不起作用。这是我的form.php和form.html:

form.php的:

<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key
        $secret = 'secret';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success):
            //contact form submission code
            $name = !empty($_POST['name'])?$_POST['name']:'';
            $email = !empty($_POST['email'])?$_POST['email']:'';
            $message = !empty($_POST['message'])?$_POST['message']:'';

            $to = 'kontakt@boakustikk.no';
            $subject = 'New contact form have been submitted';
            $htmlContent = "
                <h1>Henvendelse fra din hjemmeside</h1>
                <p><b>Navn: </b>".$name."</p>
                <p><b>E-post: </b>".$email."</p>
                <p><b>Melding: </b>".$message."</p>
            ";
            // Always set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            // More headers
            $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
            //send email
            @mail($to,$subject,$htmlContent,$headers);

            $succMsg = 'Your contact request have submitted successfully.';
        else:
            $errMsg = 'Robot verification failed, please try again.';
        endif;
    else:
        $errMsg = 'Please click on the reCAPTCHA box.';
    endif;
else:
    $errMsg = '';
    $succMsg = '';
endif;
?>

form.html:

<form action="form.php" method="POST">
    <input type="text" name="name"  placeholder="Ditt navn"  size="43" /><br><br>
    <input type="text" name="email" placeholder="Din E-post" size="43" /><br><br>
    <textarea type="text" name="message" rows="8" cols="41"></textarea><br><br>
    <div class="g-recaptcha" data-sitekey="sitekey"></div><br>
    <input type="submit" name="submit" value="SJEKK reCAPTCHA OG SEND MELDING">
</form>

1 个答案:

答案 0 :(得分:1)

您可以根据需要向$succMsg$errMsg添加文字,但不会将其回显到页面。就我所见,你也没有输出任何其他内容。因此执行form.php的最终结果将是返回浏览器的空白页。

您需要输出消息,最简单的实现是:

echo $succMsg;

echo $errMsg;

在错误的情况下。

但是当然你可能想要添加一些HTML标记和CSS来很好地格式化它,具体取决于你的要求。