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

我有一个表单,它之前从服务器发送电子邮件,但突然停止工作。我似乎无法弄清楚这个问题。当我使用没有 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>


慕沐林林
浏览 96回答 1
1回答

鸿蒙传说

你似乎错过了 Recaptcha 的秘密。如果条件正常,则删除 Recaptcha。<?php&nbsp; &nbsp; $email_to = 'ahmed_rises@hotmail.com'; //-----------> Invalid Email Id was added here&nbsp; &nbsp; $email_subject = 'form submission';&nbsp; &nbsp; $email = $_POST ['email'];&nbsp; &nbsp; $required = array('name','email','company', 'message');&nbsp; &nbsp; $form1_complete = FALSE;&nbsp; &nbsp; $validation = array();&nbsp; &nbsp; if(!empty($_POST)) {&nbsp; &nbsp; &nbsp; &nbsp; foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));&nbsp; &nbsp; &nbsp; &nbsp; foreach($required as $field) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!array_key_exists($field, $_POST)) array_push($validation, $field);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($_POST[$field] == '') array_push($validation, $field);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(count($validation) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $email_content = 'New Comment: ' . "\n\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($_POST as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Recaptca Secrets are Missing?????? Random string passed!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $recaptcha_secret = "x";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .$recaptcha_secret."&response=".$_POST['g-recaptcha-response']); //-----> Also Recapta response from&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //the form is also missing since there its not working and neither getting passed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $response = json_decode($response, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Printed the Output in which it shows the recapta Error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_r($response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If you ae to remove the Recapta Condition, the mail will be send!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if($response["success"] === true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "==========";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "email_subject:".$email_subject.", email:".$email.",email_to:".$email_to;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mail($email_to, $email_subject, $email_content, "From:" . $email);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "==========";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;echo "Failed";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'submitted';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; function validate_email_address($email = FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;&nbsp; &nbsp; }&nbsp; &nbsp; function remove_email_injection($field = FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));&nbsp; &nbsp; }?>希望能帮助到你 :)
打开App,查看更多内容
随时随地看视频慕课网APP