javascript - ajax jquery 表单停止发送电子邮件

标签 javascript php jquery ajax

我有一个表单以前从服务器发送电子邮件,但突然停止工作。我似乎无法弄清楚这个问题。当我使用没有 ajax 的表单时,电子邮件可以正常工作。我在开发工具中看到了 http 200 代码,没有错误。任何帮助将非常感激。谢谢。

html:


<form id="form1" class="form-action" action="" method="POST">

        <input type="text" name="name" id="name" class = "name" required />
        <input type="email" name="email" id="email"  required />
        <input type="text" name='company' id="company" class="company" required />
        <textarea class= "message" name="message" id="message" required /> 
        </textarea>


<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="x" data-callback="recaptcha"></div>


     <button type="submit" id="submit-button">Submit</button>
     <span class="success_message font-weight-bolder text-center hide" style="margin-left: 30px;">
                                            message received.</span>
</form>
<script>
function reset_form(){
        $("#form1")[0].reset();
    }
    function reset_success_message(){
        $('.success_message').addClass('hide');
    }
    $(document).ready(function(){
        $('#name').click(function () {
            $('.success_message').addClass('hide');
        });
        $('.email').click(function () {
            $('.success_message').addClass('hide');
        });
        $('.company').click(function () {
            $('.success_message').addClass('hide');
        });
        $('#message').click(function () {
            $('.success_message').addClass('hide');
        });

        $('#form1').submit(function (e) {
            $('.success_message').addClass('hide');
            e.preventDefault();

            $.ajax({
               url: 'serverside.php',
               type: 'post',
               data: $('#form1').serialize(),
               success: function (response) {
                    if(response == 'submitted'){
                        reset_form();
                        $('.success_message').removeClass('hide');

                    }
               }
            });
        });
    });
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


服务器端.php

<?php

$email_to = 'x@domain.com';
$email_subject = 'form submission';
$email = $_POST ['email'];

$required = array('name','email','company', 'message');


$form1_complete = FALSE;

$validation = array();

if(!empty($_POST)) {

    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

    foreach($required as $field) {

        if(!array_key_exists($field, $_POST)) array_push($validation, $field);

        if($_POST[$field] == '') array_push($validation, $field);

        if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
    }

    if(count($validation) == 0) {
        $email_content = 'New Comment: ' . "\n\n";

        foreach($_POST as $key => $value) {
            if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n ";
        }


        $recaptcha_secret = "x";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true) {

            mail($email_to, $email_subject, $email_content, "From:" . $email);

         }
        else
        {
        }
       echo 'submitted';
    }
}

function validate_email_address($email = FALSE) {
    return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function remove_email_injection($field = FALSE) {
    return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}

?>

最佳答案

您似乎缺少 Recaptcha secret 。如果 Recaptcha 工作正常,则删除它。

enter image description here

<?php

    $email_to = 'ahmed_rises@hotmail.com'; //-----------> Invalid Email Id was added here
    $email_subject = 'form submission';
    $email = $_POST ['email'];

    $required = array('name','email','company', 'message');


    $form1_complete = FALSE;

    $validation = array();

    if(!empty($_POST)) {

        foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

        foreach($required as $field) {

            if(!array_key_exists($field, $_POST)) array_push($validation, $field);

            if($_POST[$field] == '') array_push($validation, $field);

            if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
        }

        if(count($validation) == 0) {
            $email_content = 'New Comment: ' . "\n\n";

            foreach($_POST as $key => $value) {
                if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n ";
            }

            //Recaptca Secrets are Missing?????? Random string passed!
            $recaptcha_secret = "x";

            $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret="
            .$recaptcha_secret."&response=".$_POST['g-recaptcha-response']); //-----> Also Recapta response from
            //the form is also missing since there its not working and neither getting passed

            $response = json_decode($response, true);

            //Printed the Output in which it shows the recapta Error
            echo "<pre>";
            print_r($response);

            //If you ae to remove the Recapta Condition, the mail will be send!
            // if($response["success"] === true) {
                echo "==========";

                echo "email_subject:".$email_subject.", email:".$email.",email_to:".$email_to;
                mail($email_to, $email_subject, $email_content, "From:" . $email);

                echo "==========";

            // }
            // else
            // {
            //     echo "Failed";
            // }

            echo "<br>";
            echo 'submitted';
        }
    }

    function validate_email_address($email = FALSE) {
        return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
    }

    function remove_email_injection($field = FALSE) {
        return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
    }

?>

希望有帮助:)

关于javascript - ajax jquery 表单停止发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60471960/

相关文章:

javascript - VueJS 向图像添加文本

mysql - 向 Node 表达mysql同步问题的多个请求

javascript - Symfony:通过 GET 将变量从 Ajax 发送到处理程序

php - 在不同的机器上配置 php 和 nginx

javascript 与命名空间事件

javascript - JQuery 重置所有其他子元素的 z-index

javascript - 断开连接的图表上缺少悬停标记

javascript - 从当前鼠标位置绘制三 Angular 形

php - 无法打印_r domDocument

javascript - 如何仅将 jquery 应用于使用 if 语句选择的元素?