设置Ajax将表单结果发送到php文件以便发送电子邮件

时间:2013-01-20 05:10:07

标签: php jquery html ajax xmlhttprequest

我目前在this site上有一个联系表单,我希望从php结果迁移到ajax结果。我使用了一个带有以下代码的mail.php文件:

<?php
 $firstname = $_POST['firstName'];
 $lastname = $_POST['lastName']; 
 $email = $_POST['email'];
 $tel1 = $_POST['tel1'];
 $tel2 = $_POST['tel2'];
 $tel3 = $_POST['tel3'];
 $reason = $_POST['reason'];
 $message = $_POST['text'];
 $formcontent="From: $firstname $lastname \n Email address: $email \n Telephone Number: $tel1 $tel2 $tel3 \n Reason: $reason \n Message: $message";
 $recipient = "ilanbiala@ilanshomekitchen.x10.mx";
 $subject = "Contact Form";
 $mailheader = "From: $email \r\n";
 mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
 echo "<script>alert('Thank you!');</script>";
 ?>

此php文件发送了一封包含联系表单结果的电子邮件。但是,它还重新加载页面并在空白页面上显示一个警告框,这不是我想要的。我希望使用jquery ajax函数来使它,以便ajax将信息发送到我的php文件发送邮件,除非我可以直接从ajax邮件联系表单获得结果。

联系表格通过此ajax功能提交:

function submitForm(form) {
    var form = $("form").submit(function() {
        var formData = new FormData(form);
        console.log("formData");
        var XHR;
        if (window.XHMLHttpRequest){XHR = new XHMLHttpRequest();}
        else {XHR = new ActiveXObject("Microsoft.XMLHTTP");}
        XHR.open("POST","mail.php",true);
        console.log("XHR opened.");
        XHR.onreadystatechange = function(e) {
            if (this.readyState == 4 && this.status == 200) {
                XHR.send(formData);
                console.log("Sent formData.");
            }
        }
    });
}

3 个答案:

答案 0 :(得分:2)

您可以使用jQuery's .ajax

$.ajax({
        type: "POST",
        url: "mail.php",
        data: { firstname: nameVar, email: emailVar............ }
    }).done(function( msg ) {
        alert( msg );
    });

答案 1 :(得分:1)

在您关联的网页上,您的表单的ID为form,因此我将在示例代码中使用该代码:

JavaScript代码

$(document).ready(function(){
    $('#form').submit(function(e){
        e.preventDefault();

        $.post($(this).attr('action'), $(this).serialize(), function(data) {
            // do whatever with the data returned
            console.log(data);
        });
    }, 'text');
});

这是一个非常简单的示例,在HTML的<form>标记中,您需要添加操作属性(在您的情况下,它将是mail.php)。

PHP代码

<?php

 $firstname = $_POST['firstName'];
 $lastname = $_POST['lastName']; 
 $email = $_POST['email'];
 $tel1 = $_POST['tel1'];
 $tel2 = $_POST['tel2'];
 $tel3 = $_POST['tel3'];
 $reason = $_POST['reason'];
 $message = $_POST['text'];
 $formcontent="From: $firstname $lastname \n Email address: $email \n Telephone Number: $tel1 $tel2 $tel3 \n Reason: $reason \n Message: $message";
 $recipient = "ilanbiala@ilanshomekitchen.x10.mx";
 $subject = "Contact Form";
 $mailheader = "From: $email \r\n";

 if(mail($recipient, $subject, $formcontent, $mailheader)) {
     echo 'Thank you!';
 } else {
     echo 'Error!';
 }

?>

例如,您可以使用JSON尝试更加漂亮的东西,但是对于这个例子,我保持它非常简单。

答案 2 :(得分:0)

正如已经提到的,您可以使用简单的AJAX调用来实现您想要的效果。但是,而不是

echo "<script>alert('Thank you!');</script>";

我建议这样的事情:

header('Content-type: application/json');
if(mail($recipient, $subject, $formcontent, $mailheader)) {
    echo "{'msg':'email has been sent'}";
} else {
    echo "{'error':'there was an error sending email'}";
}

然后在AJAX调用中,您可以获取返回的JSON对象并在页面上显示成功/错误消息。