javascript - 使用ajax提交表单时如何发送电子邮件

标签 javascript php ajax

我在名为 buyback.php 的页面上有一个表单,该表单位于页面底部。表格的编码是:-

<div class="mktoForm" id="elqForm">
        <form id="elqForm" method="post" name="elqForm" action="buyback.php">
            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718959"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_First_Name"><strong>First Name</strong> *:</label>
                <input type="text" required="required" id="Contact_First_Name" name="Contact_First_Name" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718960"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_Last_Name"><strong>Last Name</strong> *:</label>
                <input type="text" required="required" id="Contact_Last_Name" name="Contact_Last_Name" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718961"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_Email"><strong>Business Email</strong> *:</label>
                <input type="text" required="required" pattern="[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$" id="Contact_Email" name="Contact_Email" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718962"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Job_Responsibility"><strong>Job Title</strong> *:</label>
                <input type="text" required="required" id="Job_Responsibility" name="Job_Responsibility" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718963"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Company_Name"><strong>Company Name</strong> *:</label>
                <input type="text" required="required" id="Company_Name" name="Company_Name" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718964"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Company_Country"><strong>Country</strong> *:</label>
                <input type="text" required="required" id="Company_Country" name="Company_Country" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718965"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Company_State_Province"><strong>City</strong> *:</label>
                <input type="text" required="required" id="Company_State_Province" name="Company_State_Province" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718966"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Contact_Phone_Number"><strong>Business Phone Number</strong> *:</label>
                <input type="text" required="required" id="Contact_Phone_Number" name="Contact_Phone_Number" autocomplete="off">
            </div>

            <div class="elqFormRow">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718966"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <label for="Number_Of_Employees"><strong>Number of Employees</strong> *:</label>
                <select id="Number_Of_Employees" name="Number_Of_Employees">
                    <option value="bet10and49">Between 10 and 49</option>
                    <option value="bet50and99">Between 50 and 99</option>
                    <option value="bet100and199">Between 100 and 199</option>
                    <option value="ab200">Above 200</option>
                </select>
            </div>

            <div class="elqFormRow text-center">
                <div style="line-height:0px;font-size:0px;padding:0;margin:0;max-height:0px;"><a name="eiel1718967"><img src="x.gif" width="1" height="0" style="display:block; height:0px !important;" class="anchorlink_image" alt=""></a></div>
                <div class="elqFormRow text-center">
                    <input type="submit" id="elqFormSubmitBut" name="elqFormSubmitBut" class="orange_btn" value="Submit" checkrequired="true">
                </div>
            </div>
        </form>
        <div id="result"><?php if(isset($result)){ echo $result; } ?></div>
    </div>

在那个表格中有一个 div,它是 <div id="result"></div>我想以这种形式打印一条成功消息。目前我正在阅读字段值并通过 php 发送电子邮件:-

if ($_POST['elqFormSubmitBut']) {
$Contact_First_Name = $_POST['Contact_First_Name'];
$Contact_Last_Name = $_POST['Contact_Last_Name'];
$Contact_Email = $_POST['Contact_Email'];
$Job_Responsibility = $_POST['Job_Responsibility'];
$Company_Name = $_POST['Company_Name'];
$Company_Country = $_POST['Company_Country'];
$Company_State_Province = $_POST['Company_State_Province'];
$Contact_Phone_Number = $_POST['Contact_Phone_Number'];
$Number_Of_Employees = $_POST['Number_Of_Employees'];

$to = "aaa@gmail.com";
$from = "bbb@gmail.com";

switch ($Number_Of_Employees) {
    case "bet10and49": $Number_Of_Employees = "Between 10 and 49"; break;
    case "bet50and99": $Number_Of_Employees = "Between 50 and 99"; break;
    case "bet100and199": $Number_Of_Employees = "Between 100 and 199"; break;
    case "ab200": $Number_Of_Employees = "Above 200"; break;
}

$message = "
  Contact First Name: $Contact_First_Name<br/>
  Contact Last Name: $Contact_Last_Name<br/>
  Contact Email: $Contact_Email<br/>
  Job Responsibility: $Job_Responsibility<br/>
  Company Name: $Company_Name<br/>
  City: $Company_State_Province<br/>
  Business Phone Number: $Contact_Phone_Number<br/>
  Number Of Employees: $Number_Of_Employees";

$subject = "New Message from the Buyback Form";
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'. $from  . " \r\n";

if (mail($to, $subject, $message, $header)) {
    $result = "Thank you for contacting us. We will reply you as soon as possible";
} else {
    $result = "Message Sending Failed, try again";
}

我在 div 中呼应 $result。我如何编写等效的 ajax 代码来发送电子邮件并将结果消息写入该 div?我需要有人告诉我到底要写什么

最佳答案

您有两个选择,vanilla JavaScript 或 jQuery。

HTML

您不需要对您的 HTML 进行任何重大更改,您只需添加一个 onsubmit="return function()" 属性以及您要调用的函数,在这种情况下我们将使用 sendForm()

<form id="elqForm" method="post" name="elqForm" onsubmit="return sendForm()">

JavaScript

在您的 JavaScript 代码的其他地方,您必须添加以下行:

原生 JavaScript:

function sendForm(){
   var form = document.querySelector('elqForm');
   var data = new FormData(form);
   const Http = new XMLHttpRequest();
   const url='path/to/your/phpfile.php';
   Http.open("POST", url);
   Http.send(data);
   Http.onreadystatechange = function () {
    if(Http.readyState === XMLHttpRequest.DONE && Http.status === 200) {
        document.getElementById("result").innerHTML = Http.responseText;
    }
};
return false;
}

使用 jQuery:

https://api.jquery.com/

function sendForm(){
   var data = $("#elqForm").serialize();
   $.ajax({
     type: "POST",
     url: "path/to/your/phpfile.php",
     data: data,
     success: function(response){
        $("#result").html(response);
     }
   });
}

PHP

只需更改此行以回显您要在 div 中输出的文本。

if (mail($to, $subject, $message, $header)) {
    echo "Thank you for contacting us. We will reply you as soon as possible";
} else {
    echo "Message Sending Failed, try again";
}

关于javascript - 使用ajax提交表单时如何发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55169328/

相关文章:

javascript - Flowtype:不安全的实例变量访问

php - 应用 "this"jQuery,从sql数据库中删除php中的回显行

php - 根据可用字体更改字体大小

php - 计数目录中的文件,但不包括子目录

javascript - 如何加快 JSON 日期解析速度?

javascript - 如何在不刷新的情况下通知数据发生变化?

javascript - 什么会导致元素在 Firefox 中正确显示,但在 Chrome 或 Safari 中不能正确显示?

php - 如何根据另一种颜色计算出某种颜色?

javascript - 使用 javascript 在 HTML 中导入 XML

javascript - AJAX BitMovin - GET 请求不起作用