Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX时,ReCAPTCHA不工作_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 使用AJAX时,ReCAPTCHA不工作

Javascript 使用AJAX时,ReCAPTCHA不工作,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我从我的联系方式中收到了垃圾邮件,所以我在我的联系方式中加入了谷歌的ReCAPTCHA。经过多次尝试并失败后,我正处于寻求帮助的时刻 首先,我的HTML在一个显示中,并调用ajaxsubmit.php <div class="reveal" id="contactModal" data-reveal> <h1>Contact Us</h1> <div class="grid-x grid-m

我从我的联系方式中收到了垃圾邮件,所以我在我的联系方式中加入了谷歌的ReCAPTCHA。经过多次尝试并失败后,我正处于寻求帮助的时刻

首先,我的HTML在一个显示中,并调用ajaxsubmit.php

            <div class="reveal" id="contactModal" data-reveal>
          <h1>Contact Us</h1>
          <div class="grid-x grid-margin-x">
            <form id="ajax-contact" method="post" action="/assets/js/ajaxsubmit.php" class="large-12 cell">
              <div class="grid-x grid-margin-x">
                <div class="large-6 cell">
                  <label>Name*</label>
                  <input id="name" name="name" required type="text" placeholder="Name..." /> </div>
                <div class="large-6 cell">
                  <label>Company*</label>
                  <input id="company" type="text" placeholder="Company..." name="company" required/> </div>
              </div>
              <div class="grid-x grid-margin-x">
                <div class="large-6 cell">
                  <label>Email*</label>
                  <input id="email" type="email" placeholder="Email..." name="email" required /> </div>
                <div class="large-6 cell">
                  <label>Phone*</label>
                  <input id="phone" type="tel" placeholder="Phone..." name="phone" required/> </div>
              </div>
              <div class="grid-x grid-margin-x">
                <div class="large-12 cell">
                  <label>Message*</label>
                  <textarea id="message" placeholder="Message..." name="message" required></textarea>
                </div>
              </div>
              <div class="grid-x grid-margin-x">
                  <div class="large-6 cell">
                  <div class="g-recaptcha" data-sitekey="MYSITEKEY"></div>
                  </div>
                <div class="large-6 clear-fix">
                  <button class="button float-right" type="submit">Submit</button>
                </div>
              </div>
            </form>
            <div id="form-messages"></div>
          </div>
          <button class="close-button" data-close aria-label="Close reveal" type="button"> <span aria-hidden="true">&times;</span> </button>
        </div>
js之后,它通过Ajax jquery函数$.Ajax发布到我的ajaxsubmit.php文件中

 <?php
// If the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

// If the Google Recaptcha box was clicked
if(isset($_POST[‘captcha’]) && !empty($_POST[‘captcha’])){    
$captcha=$_POST[‘captcha’];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=MYSECRET&response=".$captcha."&remoteip=".$_SERVER[‘REMOTE_ADDR’]);
$obj = json_decode($response);

// If the Google Recaptcha check was successful
if($obj->success == true) {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$company = strip_tags(trim($_POST["company"]));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$phone = strip_tags(trim($_POST["phone"]));
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
        if ( empty($name) OR empty($company) OR empty($phone) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
        // Set the recipient email address.
        $recipient = "EMAIL";

        // Set the email subject.
        $subject = "New contact from $name";

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Company: $company\n\n";
        $email_content .= "Phone: $phone\n\n";
        $email_content .= "Message:\n$message\n";

        // Build the email headers.
        $email_headers = "From: $name <$email>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "Thank You! Your message has been sent.";
}

else {
http_response_code(500);
echo "Oops! Something went wrong, and we couldn’t send your message.";
}

}

// If the Google Recaptcha check was not successful
else {
echo "Robot verification failed. Please try again.";
}

}

// If the Google Recaptcha box was not clicked
else {
echo "Please click the reCAPTCHA box.";
}

}

// If the form was not submitted
// Not a POST request, set a 403 (forbidden) response code.
else {
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>

现在,这就是我的问题所在,它永远不会超过第一个if语句ifasset$\u POST['captcha']&&!清空$_POST['captcha'],直接进入请点击ReCAPTCHA框。如果我回显captcha var,则其中包含数据。所以我不明白,如果有任何帮助,为什么它不会继续到第一个声明,如果是非常感谢。谢谢你

我认为问题在于$u POST['captcha']

从谷歌开发网站

g-recaptcha-response POST parameter when the user submits the form on your site
grecaptcha.getResponse(opt_widget_id) after the user completes the reCAPTCHA challenge
As a string argument to your callback function if data-callback is specified in either 
the g-recaptcha tag attribute or the callback parameter in the grecaptcha.render method
在recaptcha网站上,您可以找到有关如何验证recaptcha本身的详细信息:

您可以使用此选项验证ifgrecaptcha.getResponse.length!==0{console.log验证码已解决;}从错误报告中获取此错误的更多说明警告:第10行的/assets/js/ajaxsubmit.php中的allow\u url\u fopen=0在服务器配置中禁用了文件获取内容:https://wrapper警告:文件获取内容:未能打开流:在第10行的/assets/js/ajaxsubmit.php中找不到合适的包装器注意:正在尝试在第14行的/assets/js/ajaxsubmit.php中获取非对象的属性
g-recaptcha-response POST parameter when the user submits the form on your site
grecaptcha.getResponse(opt_widget_id) after the user completes the reCAPTCHA challenge
As a string argument to your callback function if data-callback is specified in either 
the g-recaptcha tag attribute or the callback parameter in the grecaptcha.render method