我希望在我的网站上提交表单后运行一些PHP代码。它重定向到另一个页面以通过电子邮件发送信息,然后使用header()返回上一页。我想要发生的是加载一条小消息,显示消息已成功发送,但我不确定如何从另一页重定向后执行此操作。我希望使用php或jquery来实现这一目标,并且我将包含我正在使用的代码。我尝试使用session_start函数来执行此操作,但在用户离开后,回显的文本仍保留在页面上。
<form method="post" action="mail.php">
<tr>
<td class="label">Name:</td>
<td class="input"><input type="text" maxlength="40" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td>
</tr>
<tr>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" name="email" title="Please enter an email address." required></td>
</tr>
<tr>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" name="subject" title="Please enter a subject." required></td>
</tr>
<tr>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" maxlength="1000" name="message" title="Please enter a message." required></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</form>
<?php
$to = "kouen922@gmail.com";
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"]));
$message = trim(htmlspecialchars($_POST["message"]));
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"]))."";
mail($to,$subject,$message,$headers);
header("Location: index.php#contact");
?>
答案 0 :(得分:0)
使用jjery post请求,而不是使用php,使用Jquery(因为你提到过它)并使用它的潜力发送你的表单数据。通过这种方式,您可以保持在同一页面而无需导航到另一个页面(因为您正在返回),并且您也可以在完成发布请求后显示消息,ajax POST方法在请求完成后返回回调 Read about it here
答案 1 :(得分:0)
易。
isset($_GET['thankyou'])
然后为{{1}}执行条件并打印出一条消息。
答案 2 :(得分:0)
根据你的描述:表单页面 - php电子邮件页面 - 返回表单页面,然后只需将php电子邮件部分添加到表单页面中:
的index.php:
<?php
$data = init_data();
if ($data['cmd']) {
$err = main_send($data);
}
if (!$data['cmd'] || $err) {
main_form($data, $err);
}
function init_data() {
$arr = ['cmd', 'name', 'email', 'subject', 'message'];
foreach ($arr as $k) {
$data[$k] = isset($_POST[$k]) ? trim(htmlspecialchars($_POST[$k])) : '';
}
return $data;
}
function main_send($data) {
if (!$data['name']) {
return 'What is your name?';
} elseif (!$data['email']) {
return 'What is your email?';
} elseif (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
return 'Your email is not valid';
} elseif (!$data['message']) {
return 'Please say something';
}
$to = 'kouen922@gmail.com';
$subject = 'Message From Your Website: ' . $data['subject'];
$message = trim(htmlspecialchars($_POST["message"]));
$headers = 'Reply To: ' . $data['email'])) . "\r\nFrom: " . $data['name']));
mail($to, $subject, $message, $headers);
echo '<p>Thank you very much for the feedback</p>';
}
function main_form($data, $err = '') {
if ($err) {
echo '<p class="err">' , $err , '</p>';
}
echo 'your original form html here ...';
}
答案 3 :(得分:0)
试用此代码:
表单代码
<div id="message"></div>
<form method="post" id="email-form" action="">
<tr>
<td class="label">Name:</td>
<td class="input"><input type="text" maxlength="40" id="name" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td>
</tr>
<tr>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" id="email" name="email" title="Please enter an email address." required></td>
</tr>
<tr>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" id="subject" name="subject" title="Please enter a subject." required></td>
</tr>
<tr>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" id="message" maxlength="1000" name="message" title="Please enter a message." required></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</form>
email.php代码
<?php
//email.php
$to = "kouen922@gmail.com";
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"]));
$message = trim(htmlspecialchars($_POST["message"]));
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"]))."";
mail($to,$subject,$message,$headers);
echo 'Mail was successfully send';
?>
Jquery代码:
<script>
$(document).ready(function () {
$("#email-form").submit(function(event){
event.preventDefault();
var email= $('#email').val();
var name= $('#name').val();
var subject= $('#subject').val();
var messege = $('#messege').val();
$.ajax({
url: "email.php",
type:"POST",
data: 'email='+email + '&name='+name +'&subject='+subject+'&message='+message,
success: function(data){
$('#message').html(data);
setTimeout(function(){ //redirect after 5 sec
window.history.go(-1); // Simulates a back button click
},5000); // time in milli sec
}
});
});
});
</script>