重新捕获后如何执行行动表格

时间:2018-10-25 18:57:16

标签: php forms recaptcha

我想使用我的表单发布评论,它无需重新输入便可以工作,但是当我添加重新输入时,这里不起作用是我使用重新输入的代码

<?php
           echo "<form class='recaptchaForm' method='POST' action='".setComments($conn)."'>
              <input type='hidden' name='room_name' value='$name'>
              <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
              <input type='text' name='uname' id='uname' class='name' placeholder='Name' required  /><br>
              <input type='text' name='umail'
              id='umail' class='name' placeholder='E-mail' required  />

              <textarea id='message' class='message' name='message'
              placeholder='Join the Discussion'
              required ></textarea><br>

              <div class='g-recaptcha' data-sitekey='6LfF4XYUAAAAAMaLkn2-AMUsoEkt-ym-SFsCVEBG'></div>

              <button type='submit' id='post' name='commentSubmit'>Post Review</button>

            </form>";
        ?>

这是脚本

<script>
    $(document).on('click', '[name="commentSubmit"]' function(event)  {

    var recaptcha = $('[name="g-recaptcha-response"]').val();
    if(recaptcha===""){

    event.preventDefault();
    alert("Please Check Recaptcha");
 }
  event.preventDefault();
   $.post("submit.php",{
   "secret":"6LfF4XYUAAAAAFXOxblX4Vttp67bmaRJXkua6Ks-",
   "response":recaptcha
 },function(response){
 console.log(response);

 $('.recaptchaForm').submit();

   }); // End of response

 }); // End of $.post
}); // End of click event function
</script>

这是Submit.php

<?php
   $secret = $_POST["secret"];
   $response = $_POST["response"];
   $url = "https://www.google.com/recaptcha/api/siteverify? 
   secret=".$secret."&response=".$response;
   $verify = file_get_contents($url);
   echo $verify;
?>

这是action ='“。setComments($ conn)。”'

function setComments($conn){
    if(isset($_POST['commentSubmit'])){
        $room_name = $_POST['room_name'];
        $date = $_POST['date'];
        $uname = $_POST['uname'];
        $umail = $_POST['umail'];
        $message = $_POST['message'];

        $query = "insert into comments_table (cname, cmail, cmessage,room_name,cdate) Values ('$uname','$umail','$message','$room_name','$date')";
        $result = mysqli_query($conn, $query);
    }
    } 

我认为我错过了一些东西,但我不知道那是什么 请帮助我成为初学者

1 个答案:

答案 0 :(得分:0)

我认为event.preventDefault不允许表单提交。因此,将脚本修改为此:

$(document).on('click', '[name="commentSubmit"]', function(event)  {
event.preventDefault();
var recaptcha = $('[name="g-recaptcha-response"]').val();
if(recaptcha===""){

  alert("Please Check Recaptcha");

}
else{

 $.post("submit.php",{
    "secret":"6LfF4XYUAAAAAFXOxblX4Vttp67bmaRJXkua6Ks-",
    "response":recaptcha
 },function(response){
    console.log(response);

    $('.recaptchaForm').submit();


   });
 }

 });