Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.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 如何将reCAPTCHA与我的表单联系人一起使用?_Javascript_Php_Ajax_Recaptcha - Fatal编程技术网

Javascript 如何将reCAPTCHA与我的表单联系人一起使用?

Javascript 如何将reCAPTCHA与我的表单联系人一起使用?,javascript,php,ajax,recaptcha,Javascript,Php,Ajax,Recaptcha,当我按下表单上的Submit按钮时,reCAPTCHA将允许。 我的表单可以不经检查发送消息我不是机器人。 我不知道是不是关于包含ajax。 如何将reCAPTCHA与我的表单联系人一起使用 非常感谢您在这件事上的时间和帮助 这是我的HTML代码 <form class="callus" onSubmit="return false"> <div id="result"></div> <input type="text" name="name"

当我按下表单上的Submit按钮时,reCAPTCHA将允许。 我的表单可以不经检查发送消息我不是机器人。 我不知道是不是关于包含ajax。 如何将reCAPTCHA与我的表单联系人一起使用

非常感谢您在这件事上的时间和帮助

这是我的HTML代码

<form class="callus" onSubmit="return false">
  <div id="result"></div>
  <input type="text" name="name" id="name">
  <input type="email" name="email" id="email">
  <textarea name="message" id="message"></textarea>
  <div class="g-recaptcha" data-sitekey="XXXXX"></div>
  <button id="submit_btn">Submit</button>
</form>

提交
这是我的Javascript

jQuery(document).ready(function($){
  $("#submit_btn").click(function() {
    var user_name       = $('input[name=name]').val();
    var user_email      = $('input[name=email]').val();
    var user_message    = $('textarea[name=message]').val();

    var post_data, output;
    var proceed = true;
      if(user_name==""){
        proceed = false;
      }
      if(user_email==""){
        proceed = false;
      }
      if(user_subject==""){
        proceed = false;
      }
      if(user_message==""){
        proceed = false;
      }

      if(proceed) {
        post_data = {'userName':user_name, 'userEmail':user_email, 'userMessage':user_message};

        $.post('email.php', post_data, function(response){

          if(response.type == 'error') {
            output = '<div class="alert-danger">'+response.text+'</div>';
          }else{
            output = '<div class="alert-success">'+response.text+'</div>';
            $('.callus input').val('');
            $('.callus textarea').val('');
          }
        $("#result").hide().html(output).slideDown();
      }, 'json');
    }
  });

  $(".callus input, .callus textarea").keyup(function() {
    $("#result").slideUp();
  });
});
jQuery(文档).ready(函数($){
$(“#提交”_btn”)。单击(函数(){
var user_name=$('input[name=name]')。val();
var user_email=$('input[name=email]')。val();
var user_message=$('textarea[name=message]')。val();
var post_数据,输出;
var=true;
如果(用户名==“”){
继续=错误;
}
如果(用户\电子邮件==“”){
继续=错误;
}
如果(用户\主题==“”){
继续=错误;
}
如果(用户消息==“”){
继续=错误;
}
如果(继续){
post_data={'userName':user_name,'userEmail':user_email,'userMessage':user_message};
$.post('email.php',post_数据,函数(响应){
如果(response.type==“error”){
输出=''+响应。文本+'';
}否则{
输出=''+响应。文本+'';
$('.callus input').val('');
$('.callus textarea').val('');
}
$(“#结果”).hide().html(输出).slideDown();
}“json”);
}
});
$(“.callus输入,.callus文本区域”).keyup(函数(){
$(“#结果”).slideUp();
});
});
email.php

<?php
  if($_POST) {
    $to_Email       = 'demo@localhost.com';
    $subject        = 'New Contact Inquiry';

    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

      $output = json_encode(
        array(
          'type'=>'error',
          'text' => 'Request must come from Ajax'
      ));
      die($output);
    }

    if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"])) {
      $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
      die($output);
    }

    $user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
    $user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
    $user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

    if(strlen($user_Name)<3) {
      $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
      die($output);
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) {
      $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
      die($output);
    }

    if(strlen($user_Message)<5) {
      $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
      die($output);
    }

    $message_Body = "<strong>Name: </strong>". $user_Name ."<br>";
    $message_Body .= "<strong>Email: </strong>". $user_Email ."<br>";
    $message_Body .= "<strong>Message: </strong>". $user_Message ."<br>";

    $headers = "From: " . strip_tags($user_Email) . "\r\n";
    $headers .= "Reply-To: ". strip_tags($user_Email) . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $sentMail = @mail($to_Email, $subject, $message_Body, $headers);

    if(!$sentMail) {
      $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
      die($output);
    }else{
      $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .' Thank you for contacting us.'));
      die($output);
    }
  }
?>

您必须在服务器端验证验证码。一旦你检查了验证码,它会给你验证码

var captchaCode = this.grecaptcha.getResponse();
提供回调函数,如

<div class="g-recaptcha" id="headerCaptcha" data-callback="recaptchaCallbackHeader" data-expired-callback="recaptchaExpiryHeader" data-sitekey="xxx"></div>
响应此API,您将获得

  $responseKeys = json_decode($response,true);
    if(intval($responseKeys["success"]) !== 1) {
      echo '<h2>You are not a robot  @$%K out</h2>';
    } else {
      echo '<h2>Thanks for posting comment.</h2>';
    }
$responseKeys=json\u decode($response,true);
if(intval($responseKeys[“success”])!==1){
echo“你不是机器人@$%K out”;
}否则{
echo“感谢您发表评论”;
}
请记住,recaptcha提供两种类型的密钥

  • 在服务器端用于验证验证码代码的私钥
  • 用于在客户端呈现验证码的站点密钥

  • 您必须在服务器端验证验证码。一旦你检查了验证码,它会给你验证码

    var captchaCode = this.grecaptcha.getResponse();
    
    提供回调函数,如

    <div class="g-recaptcha" id="headerCaptcha" data-callback="recaptchaCallbackHeader" data-expired-callback="recaptchaExpiryHeader" data-sitekey="xxx"></div>
    
    响应此API,您将获得

      $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          echo '<h2>You are not a robot  @$%K out</h2>';
        } else {
          echo '<h2>Thanks for posting comment.</h2>';
        }
    
    $responseKeys=json\u decode($response,true);
    if(intval($responseKeys[“success”])!==1){
    echo“你不是机器人@$%K out”;
    }否则{
    echo“感谢您发表评论”;
    }
    
    请记住,recaptcha提供两种类型的密钥

  • 在服务器端用于验证验证码代码的私钥
  • 用于在客户端呈现验证码的站点密钥