为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕沐林林 2022-10-14 10:46:11
我有一个表单,它之前从服务器发送电子邮件,但突然停止工作。我似乎无法弄清楚这个问题。当我使用没有 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>
查看完整描述

1 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

你似乎错过了 Recaptcha 的秘密。如果条件正常,则删除 Recaptcha。

//img1.sycdn.imooc.com//6348cd9e0001462a09890378.jpg

<?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));

    }


?>

希望能帮助到你 :)


查看完整回答
反对 回复 2022-10-14
  • 1 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信