我有一个非ajax PHP表单验证和邮件发送页面。发送邮件或出现验证错误时,页面将重定向到另一个空白页面,其中包含回显的成功或失败消息。是否可以在自定义设计的页面(即具有页眉,页脚,菜单等的页面)上完成消息的重定向?
答案 0 :(得分:1)
您可以将消息状态保存在$_SESSION
中以记住状态(例如,错误,成功等)。这样,通过再次删除$_SESSION
,可以确保成功/错误消息页面仅被访问一次。
要重定向,请使用
header("Location: path/to/your/html/page");
最后,您的代码可能如下所示:
page1.php
session_start();
if(form submitted){
if(no errors){
$_SESSION['status'] = 0; // 0 for no error
}else{
$_SESSION['status'] = 1; // 1 for some other error. Extend it as you prefer
}
// Redirect to second page with header, body, footer,etc.
header("Location: page2.php");
}
page2.php
session_start();
if(!isset($_SESSION['status']){
header("Location: /"); // redirect if no status has been set yet
}
// if we reach this line, we have a status and the user should be able
// to read it once
if($_SESSION['status'] == 0){
print "Hey nice, no errors!";
}else{
print "Oh no, something went wrong!";
}
// delete session at the end
unset($_SESSION['status']);