doumu5023 2018-10-25 18:57
浏览 35
已采纳

如何在recaptcha之后执行动作表单

i want to post a comment using my form, it works without a recaptcha but when i add the recaptcha it doesn't work here is my code with 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>";
        ?>

here is the script

<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>

here is the 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;
?>

here is the 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);
    }
    } 

i think i missing something but i dont know what it is please help me im a beginner

  • 写回答

1条回答 默认 最新

  • douzaipou3327 2018-10-25 19:42
    关注

    I think event.preventDefault is not letting the form to submit. So check by modifying the script to this :

    $(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();
    
    
       });
     }
    
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符