invisible recaptcha v2 不检查必填字段

invisible recaptcha v2 not checking required input fields

我正在添加一个不可见的验证码;由于某种原因,它不再检查必填字段。以前,我会收到来自 Chrome 的通知,指出字段不能为空。现在,它只是忽略了这一点,让我提交空表单。

我正在使用 ajax 提交请求。我的猜测是 data-callback 并不意味着用于发送 ajax 请求。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Submit a quote</title>
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/forms.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="../js/quotes/ajax.js"></script>
    <meta name="description" content="Submit your awesome quote today!">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="theme-color" content="#0aff0a">
    <meta name="msapplication-navbutton-color" content="#0aff0a">
    <meta name="apple-mobile-web-app-status-bar-style" content="#0aff0a">
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
    <div id="status-message"></div>
    <div class="container">
        <form id="form" action="../php/quoteSubmit.php" method="post">
            <h3>Submit your quote</h3>
            <fieldset>
                <textarea rows="20" name="quote" title="quotes" placeholder="Type your quote here...." tabindex="5" required></textarea>
            </fieldset>
            <fieldset>
                <input placeholder="Author" name="author" title="quotes" type="text" tabindex="1" required>
            </fieldset>
            <fieldset>
                <!--<button name="submit" type="submit" id="contact-submit">Submit</button>-->
                <button name="submit" type="submit" id="contact-submit" class="g-recaptcha" data-callback="onSubmit" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI">Submit</button>
            </fieldset>
        </form>
    </div>
    <script>
        function onSubmit(e) {
            //$("#form").submit(function (e) {
                var url = $("#form").attr('action'); // the script where you handle the form input.
                ajaxSendData(url, $("#form").serialize());
            //});
        }
    </script>
</body>
</html>

似乎提交操作也被取消了。

$("#form").submit(function (e) {
    var url = $("#form").attr('action');
    ajaxSendData(url, $("#form").serialize());
    e.preventDefault();
});

以上片段仅在 使用 recaptcha API 时触发。

如何正确地将 ajax 请求发送到 PHP 页面?

我的完整代码(如果需要):

<?php
include_once "connect.php";
session_start();
if(isset($_POST["quote"], $_POST["author"], $_POST["g-recaptcha-response"])){
    if(!isValid()){
        http_response_code(400);
        exit();
    }

    $stmt = $conn->prepare("INSERT INTO pending (quote, author) VALUES (?, ?)");
    $stmt->bind_param("ss", $quote, $author);

    $quote = htmlspecialchars($_POST["quote"]);
    $author = htmlspecialchars($_POST["author"]);
    $stmt->execute();

    echo"Added to pending! Thank you for submitting";
    $stmt->close();
    $conn->close();
    //$_SESSION["lastRequest"] = time();
}else{
    http_response_code(400);
    exit();
}
function isValid()
{
    try {
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = ['secret'   => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
            'response' => $_POST['g-recaptcha-response'],
            'remoteip' => $_SERVER['REMOTE_ADDR']];

        $options = [
            'http' => [
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data)
            ]
        ];

        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        if($decoded = json_decode($result, true)){
            return $decoded['success'];
        }
    }
    catch (Exception $e) {
        return null;
    }
}

尝试将recaptcha放在单独的div

<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>

function onSubmit(e) {
   //$("#form").submit(function (e) {
   var url = $("#form").attr('action'); // the script where you handle the form input.
   ajaxSendData(url, $("#form").serialize());
   //});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
    
<div id="status-message"></div>
<div class="container">
 <form id="form" action="../php/quoteSubmit.php" method="post">
  <h3>Submit your quote</h3>
  <fieldset>
   <textarea rows="20" name="quote" title="quotes" placeholder="Type your quote here...." tabindex="5" required></textarea>
  </fieldset>
  <fieldset>
   <input placeholder="Author" name="author" title="quotes" type="text" tabindex="1" required>
  </fieldset>
  <fieldset>
   <!--<button name="submit" type="submit" id="contact-submit">Submit</button>-->
      <div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
   <button name="submit" type="submit" id="contact-submit" data-callback="onSubmit">Submit</button>
  </fieldset>
 </form>
</div>