繁体   English   中英

Ajax 联系表单脚本不会在电子邮件中发送消息

[英]Ajax Contact Form Script Doesn't Send the Message in the Email

所以我在 ajax 中编写了一个脚本,该脚本连接到 PHP mail() 脚本以向info@example.com发送电子邮件(显然用我的域替换了 example.com)并且电子邮件发送正常,除了一个问题外,一切都设置好了:

     $.ajax({
        url:'contact.php',
        type:'POST',
        data: {
           name:name,
           email:email,
           message:message
          },
        dataType: 'json',
        success: function(response){
            alert(response);
        },
        error: function(response){
            alert("Oops, something went wrong. Please try again.");
        }
    })
    return false;
    event.preventDefault();
    })

我收到的电子邮件的格式与我的 contact.php 声明的完全一样:

<?php
    $to = "info@example.com";
    $from = $_POST['email'];
    $name = $_POST['name'];
    $message =$_POST['message'];
    echo $_POST['test'];
    $subject = "Contact form from: ".$name;
    $body = 'Hello developer(s)!<br><br>
The following is a submission from the Contact Form on <a target="_blank" href="https://example.com">example.com</a>. Please respond using your own email (like info@example.com).<br><br>
Name: '.$name."<br>
Email: ".$from."<br>
Message: <br>".$message."<br><br>
Thank you!";
    $headers = "From: ".$name." <".$from."> \r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail($to,$subject,$body,$headers);
    echo "Success!<br></br>";
?>

输出以下内容:

Hello developer(s)!

The following is a submission from the Contact Form on example.com. Please respond using your own email (like info@example.com).

Name: Owen Sullivan
Email: sulliops@gmail.com
Message: 


Thank you!

请注意,即使已在表单中提交了一条消息,该消息也丢失了。 知道为什么这是唯一缺少的部分吗?

编辑:这是表单代码:

<form method="post" name="cform" id="cform" action="contact.php" class="contact-form">
    <div class="row">
        <div class="col-sm-6">
            <input name="name" id="name" type="text" class="form-control" placeholder="Your Name..." required>
        </div>
        <div class="col-sm-6">
            <input name="email" id="email" type="email" class="form-control" placeholder="Your Email..." required>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <textarea name="message" id="message" cols="" rows="4" class="form-control" placeholder="Your Message..." required></textarea>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-12 text-right">
            <input type="submit" id="submit" name="send" class="submitBnt btn btn-custom" value="Send Message">
        </div>
    </div>
    <div id="simple-msg"></div>
</form>

固定的 AJAX 代码:

$.ajax({
        url:'contact.php',
        type:'POST',
        data: $(this).serialize()
        dataType: 'json',
        success: function(response){
            alert(response);
        },
        error: function(response){
            alert("Oops, something went wrong. Please try again.");
        }
    })
    return false;
    event.preventDefault();
    })

您需要检查从何处获取“消息” textarea的值。 确保您使用的是.val().value 您可能在所有表单字段上使用.text()并且它对您的textarea失败。

以下是我建议的一些更改

// You have a syntax error on yours, you need a listener
$(document).ready(function() {
    /** Listen for submit **/
    $("#cform").on("submit",function(event) {
        // Stop form from submitting normally
        event.preventDefault();
        // Send ajax
        $.ajax({
            url: 'contact.php',
            type: 'POST',
            // Serialize the form
            data: $(this).serialize(),
            // Remove the json
            success: function(response){
                alert(response);
            },
            error: function(response){
                alert("Oops, something went wrong. Please try again.");
            }
        });
    });
});

然后在你的 PHP 中会是这样的:

    <?php
    $to      = "info@example.com";
    $from    = trim($_POST['email']);
    # Stop if you don't have a valid address
    if(!filter_var($from,FILTER_VALIDATED_EMAIL))
        die('Invalid email address.');
    # Strip out stuff from the submission
    $name    = htmlspecialchars(strip_tags(trim($_POST['name'])));
    $message = htmlspecialchars(strip_tags(trim($_POST['message'])));

    $subject = "Contact form from: ".$name;
    $body    = 'Hello developer(s)!<br /><br />
The following is a submission from the Contact Form on <a target="_blank" href="https://example.com">example.com</a>. Please respond using your own email (like info@example.com).<br><br>
Name: '.$name."<br>
Email: ".$from."<br>
Message: <br />".$message."<br /><br />
Thank you!";
    $headers = "From: ".$name." <".$from."> \r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    # You need to make a condition to make sure it does, in fact, send
    echo (mail($to,$subject,wordwrap($body, 70, "\r\n"),$headers))? 'Success!' : 'Failed to send message.';


编辑:

注意:您有两个 ajax 提交,一个是您要添加的,另一个是在<script src="js/app.js"></script>文件中从第76行开始的。 您正在重复添加它的工作。 您将要编辑该块...或将其删除并构建上述内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM