由Recaptcha检查后如何从HTML表单发送数据?

时间:2019-06-03 10:30:52

标签: javascript php html forms

我正在一个联系网站上,我想在其中获得联系表。我希望它将数据发送到电子邮件,并希望由Google的recaptcha v3进行检查。

这是我的第二次尝试。过去,我已经成功完成了此任务而无需重新输入代码。现在,我使用了此(https://codeforgeek.com/google-recaptcha-v3-tutorial/)教程,结果如下:

表格下方的脚本

       // when form is submit
    $('#myform').submit(function() {
        // we stoped it
        event.preventDefault();
        var mail = $('#email').val();
        var comment = $("#sprava").val();
        // needs for recaptacha ready
        grecaptcha.ready(function() {
            // do request for recaptcha token
            // response is promise with passed token
            grecaptcha.execute('__SITE-KEY__', {action: 'create_comment'}).then(function(token) {
                // add token to form
                $('#myform').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
                    $.post("form.php",{mail: mail, comment: comment, token: token}, function(result) {
                            if(result.success) {
                                    alert('Thanks for message')
                            } else {
                                    alert('An error occured')
                            }
                    });
            });;
        });
  });
  </script>

html表单字段的名称为“电子邮件”,“ vyber”,“ sprava”

form.php

<?php
  $mail;$comment;$captcha;
  $mail = filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL);
  $comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);
  $captcha = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
  }

  function email_sending(){
    $webmaster_email = "bla@bla.com";
    $sender_email= "blabla@bla.com" ;

    $email_address = $_REQUEST['email'] ;
    $selection = $_REQUEST['vyber'] ;
    $message = $_REQUEST['sprava'];

    $msg =
    "E-mail: " . $email_address . "\r\n" .
    "I'm interested in " . $selection . "\r\n" .
    "Message: " . $message ;


    mail( "$webmaster_email", "You have mail", $msg, $header);
  }

  if($responseKeys["success"]) {
    echo json_encode(array('success' => 'true'));
    email_sending();
  } else {
    echo json_encode(array('success' => 'false'));
  }
?>

问题不属于Recaptcha部分,但随后我收到一封电子邮件,其中缺少数据。 (它仅显示变量名称,不显示实际值)。我可能认为这是因为脚本中的命名,因为我不确定在变量声明中要写什么。我很高兴收到有关此问题的任何意见。

1 个答案:

答案 0 :(得分:0)

由于这个Recaptcha tutorial,我设法通过更改如下的服务器端代码来解决了这个问题。

  // Check if form was submitted:
  if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recaptcha_response'])) {

// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = '__SECRET-KEY___';
$recaptcha_response = $_POST['recaptcha_response'];

// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);

// Take action based on the score returned:
if ($recaptcha->success == true) {
    // Verified - send email

} else {
    // Not verified - show form error

}

}