Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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
Php 如何在提交的表单中显示recaptcha错误';s页?_Php_Html - Fatal编程技术网

Php 如何在提交的表单中显示recaptcha错误';s页?

Php 如何在提交的表单中显示recaptcha错误';s页?,php,html,Php,Html,我正在联系人表单上使用recaptcha小部件,我想知道您如何才能在同一页面上显示用户可能遇到的错误,而不是转到另一页面并显示错误。您可以将表单提交到同一页面上,并在PHP端检查post数据。 检查此示例: <?php $captcha = $_POST['captcha']; if(isset($captcha)) { $captcha_answer = // you need to put your correct captcha answer here if($captch

我正在联系人表单上使用recaptcha小部件,我想知道您如何才能在同一页面上显示用户可能遇到的错误,而不是转到另一页面并显示错误。

您可以将表单提交到同一页面上,并在PHP端检查post数据。 检查此示例:

<?php 
$captcha = $_POST['captcha'];
if(isset($captcha)) {
  $captcha_answer = // you need to put your correct captcha answer here
  if($captcha == $captcha_answer) {
      // captcha is correct, continue.
  }
  else {
      // captcha is not correct, display error message.
      echo '<div style="background-color:red; padding:3px;">Your captcha is not correct, please try again.</div>';
      echo '<form method="post">
                <input type="text" name="captcha" value="Type captcha here" />
            </form>'; // print the page again
  }
}
else {
?>

<form method="post">
    <input type="text" name="captcha" value="Type captcha here" />
</form>

<?php } ?>

另一种选择是将JavaScript与AJAX结合使用。

输入“正确验证码”并单击“检查”按钮


html:

<input type="text" id="captcha"/>                 <!-- textbox to hold captcha text -->
<button id="check-captcha">check</button>         <!-- button to check captcha -->
<div id="error-message"></div>                    <!-- to show error message -->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#check-captcha").on("click", function() {  // On button click
            $.ajax({                                  // Call PHP script to check captcha
                url: "checkCaptcha.php",
                method: "post",
                data: {
                    "captcha": $("#captcha").val()    // Textbox holding captcha text
                }
            }).done(function(error) {                 // On PHP script finish..
                $("#error-message").text(error)       // Show correct/wrong message
            });             
        });
    });
</script>
<?php
    $captcha = $_POST['captcha'];           // Get captcha text from textbox
    if ($captcha == 'CORRECT-CAPTCHA') {    // If text == correct captcha
        echo 'correct';                     // Show message
    } else {
        echo 'error';                       // Show error
    }
?>

检查
$(文档).ready(函数(){
$(“#检查验证码”)。在(“单击”上,函数(){//on按钮单击
$.ajax({//调用PHP脚本检查验证码
url:“checkCaptcha.php”,
方法:“张贴”,
数据:{
“captcha”:$(“#captcha”).val()//包含captcha文本的文本框
}
}).done(函数(错误){//在PHP脚本完成时。。
$(“#错误消息”).text(错误)//显示正确/错误消息
});             
});
});
php:

<input type="text" id="captcha"/>                 <!-- textbox to hold captcha text -->
<button id="check-captcha">check</button>         <!-- button to check captcha -->
<div id="error-message"></div>                    <!-- to show error message -->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#check-captcha").on("click", function() {  // On button click
            $.ajax({                                  // Call PHP script to check captcha
                url: "checkCaptcha.php",
                method: "post",
                data: {
                    "captcha": $("#captcha").val()    // Textbox holding captcha text
                }
            }).done(function(error) {                 // On PHP script finish..
                $("#error-message").text(error)       // Show correct/wrong message
            });             
        });
    });
</script>
<?php
    $captcha = $_POST['captcha'];           // Get captcha text from textbox
    if ($captcha == 'CORRECT-CAPTCHA') {    // If text == correct captcha
        echo 'correct';                     // Show message
    } else {
        echo 'error';                       // Show error
    }
?>


我假设您是AJAX新手,下面是关于发生了什么的解释:

<input type="text" id="captcha"/>                 <!-- textbox to hold captcha text -->
<button id="check-captcha">check</button>         <!-- button to check captcha -->
<div id="error-message"></div>                    <!-- to show error message -->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#check-captcha").on("click", function() {  // On button click
            $.ajax({                                  // Call PHP script to check captcha
                url: "checkCaptcha.php",
                method: "post",
                data: {
                    "captcha": $("#captcha").val()    // Textbox holding captcha text
                }
            }).done(function(error) {                 // On PHP script finish..
                $("#error-message").text(error)       // Show correct/wrong message
            });             
        });
    });
</script>
<?php
    $captcha = $_POST['captcha'];           // Get captcha text from textbox
    if ($captcha == 'CORRECT-CAPTCHA') {    // If text == correct captcha
        echo 'correct';                     // Show message
    } else {
        echo 'error';                       // Show error
    }
?>
“AJAX调用”只是用于从JavaScript执行PHP脚本

  • 点击按钮
  • 执行的JavaScript代码
  • 完成AJAX调用以执行PHP脚本
  • 执行PHP脚本,并将消息返回到JavaScript
  • 在div元素中显示消息

  • 您可以使用ajax发送响应并返回成功/失败状态。不过,我不确定这会造成什么样的安全漏洞。我该怎么做呢?最简单的方法可能是看一看:这是我的php代码: