Php Ajax联系人表单脚本没有';不要在电子邮件中发送消息

Php Ajax联系人表单脚本没有';不要在电子邮件中发送消息,php,ajax,email,Php,Ajax,Email,因此,我用ajax编写了一个脚本,它连接到一个PHP mail()脚本,向info@example.com(显然将example.com替换为my domain)并且电子邮件发送正常,除了一个问题外,一切都已设置好: $.ajax({ url:'contact.php', type:'POST', data: { name:name, email:email, messag

因此,我用ajax编写了一个脚本,它连接到一个PHP mail()脚本,向
info@example.com
(显然将example.com替换为my domain)并且电子邮件发送正常,除了一个问题外,一切都已设置好:

     $.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();
    })
我收到的电子邮件的格式与my 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>";
?>
请注意,即使已在表单中提交了消息,该消息仍然丢失。知道为什么这是唯一缺失的部分吗

编辑:这是表单代码:

<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>

您需要检查从何处获取“message”
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.';

你能澄清问题吗?你说的“输出”是什么意思?是发到你的电子邮件的文本还是什么?你的表单元素没有name元素、值,或者与
消息
的名称不同。变量转储($\u POST);并验证您是否确实收到了一个值。是的,当发送电子邮件时,消息字段不包括我在表单页面上编写和提交的文本。此外,您可能希望从用户输入中删除html。精明的用户可以操纵您的
邮件()。在邮件和姓名上使用
strip_tags()
和可能的
htmlspecialchars()
,然后使用
$is_valid\u email=filter_var($\u POST['email'],filter\u VALIDATE\u email)
在发送电子邮件之前检查电子邮件是否有效。另外,可能会在ajax中注释掉
//dataType:'json',
,然后发送一个常规请求。经过良好教育的猜测,不确定为什么会被否决。我不知道为什么会被否决,我当然没有这样做。我最近才开始在堆栈溢出上发帖。这不是我提交的第一个自动被否决的答案…我不确定交易是什么。我算出了@TotalyQuiche-代表人数少于15人的用户从-1票开始,出于某种原因谢谢,@OwenSullivan!好的,所以首先,即使我更改了电子邮件以匹配我希望发送响应的电子邮件,也没有发送/接收任何内容。第二,我如何摆脱弹出窗口?我只想用它成功!我以前收到的消息。看我的编辑,你正在复制你的脚本。
消息
以及我们一直在讨论的
js/app.js
文件中……我从
app.js
中删除了副本,但我仍然收到两封电子邮件(一封有消息,一封没有)。我得到了弹出窗口和
成功显示在表单顶部的消息。清除浏览器缓存,重新加载页面。如果在那之后你仍然得到两个,那么你仍然有一个dup在某处
// 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
    $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.';