我在我的网站上使用form-to-email.php作为联系表单,但我并不真正理解php代码。
即用型form-to-email.php文件有一行“重定向到感谢页面”,我必须为该操作构建一个感谢页面。
但是我希望用户体验可以像弹出一个感谢消息而不是另一个页面更容易。
任何人都可以帮我创建线条?非常感谢!!
下面是form-to-email.php的完整代码,行“header('Location:thank-you.html')”是重定向路径,我想知道是否有任何方法可以修改行呢?
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$agree = $_POST['agree'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'receiver@gmail.com';//<== update the email address
$email_subject = "letter from customer";
$email_body = "$name\n".
"Message:\n$message\nLINE ID: $line".
$to = "receiver@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
答案 0 :(得分:1)
您也可以使用以下AJAX脚本来处理它。它不会重新加载页面,它将为您提供良好的用户体验。您必须包含jquery库才能工作。
$.ajax({
url: "ready-to-use form-to-email.php",
type: "post",
data: {id:xyz},
success: function (response) {
// you will get response from your php page (what you echo or print)
alert('Success') ;
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
答案 1 :(得分:1)
如果您想使用JavaScript,可以使用此代码在alert()
框中显示“感谢您的留言”消息:
将header('Location: thank-you.html')
替换为:
echo'
<script>
window.onload = function() {
alert("Thank you for your message");
location.href = "index.php";
}
</script>
';