php代码在表单提交时给我一个错误

时间:2018-12-14 01:16:17

标签: php html json recaptcha captcha

我遵循了有关Google验证码的教程,并有一个简单的表格。我将所有信息插入其中,提交表格时出现错误。我相信它可以处理最后的代码块,但可能是错误的。

PHP代码     

    $user_name      = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
    $user_email     = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $user_phone     = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
    $content   = filter_var($_POST["content"], FILTER_SANITIZE_STRING);

    if(empty($user_name)) {
        $empty[] = "<b>Name</b>";       
    }
    if(empty($user_email)) {
        $empty[] = "<b>Email</b>";
    }
    if(empty($user_phone)) {
        $empty[] = "<b>Phone Number</b>";
    }   
    if(empty($content)) {
        $empty[] = "<b>Comments</b>";
    }

    if(!empty($empty)) {
        $output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));
        die($output);
    }

    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
        $output = json_encode(array('type'=>'error', 'text' => '<b>'.$user_email.'</b> is an invalid Email, please correct it.'));
        die($output);
    }

    //reCAPTCHA validation
    if (isset($_POST['g-recaptcha-response'])) {

        require('component/recaptcha/src/autoload.php');        

        $recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());

        $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

          if (!$resp->isSuccess()) {
                $output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
                die($output);               
          } 
    }

    $toEmail = "myemail@gmail.com";
    $mailHeaders = "From: " . $user_name . "<" . $user_email . ">\r\n";
    if (mail($toEmail, "Contact Mail", $content, $mailHeaders)) {
        $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .', thank you for the comments. We will get back to you shortly.'));
        die($output);
    } else {
        $output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
        die($output);
    }
}
?>

我得到的错误是

  

无法发送电子邮件,请联系SENDER_EMAIL

这显然在“ else”语句中。我在上面constant.php中引用的文件中有Google catpcha密钥。

HTML代码:

<h1>Contact Form</h1>
<<p id="welcome">Send your comments through this form and we will get back to you. </p>
    <div id="central">
        <div class="content">
            <div id="message">
            <form id="frmContact" action="contact.php" method="POST" novalidate="novalidate">
                <div class="label">Name:</div>
                <div class="field">
                    <input type="text" id="name" name="name" placeholder="enter your name here" title="Please enter your name" class="required" aria-required="true" required>
                </div>
                <div class="label">Email:</div>
                <div class="field">         
                    <input type="text" id="email" name="email" placeholder="enter your email address here" title="Please enter your email address" class="required email" aria-required="true" required>
                </div>
                <div class="label">Phone Number:</div>
                <div class="field">         
                    <input type="text" id="phone" name="phone" placeholder="enter your phone number here" title="Please enter your phone number" class="required phone" aria-required="true" required>
                </div>
                <div class="label">Comments:</div>
                <div class="field">         
                    <textarea id="comment-content" name="content" placeholder="enter your comments here"></textarea>            
                </div>
                <div class="g-recaptcha" data-sitekey="6Le8bIEUAAAAAO9jdjwAUk57TcIP3fR4WnmVxyWm"></div>     
                <div id="mail-status"></div>            
                <button type="Submit" id="send-message" style="clear:both;">Send Message</button>
            </form>
            <div id="loader-icon" style="display:none;"><img src="img/loader.gif" /></div>
            </div>      
        </div><!-- content -->
    </div><!-- central -->  

0 个答案:

没有答案