我有一个网页,在选择后显示内容,它包含一个带有提交按钮的表单。单击提交按钮后,我想在上一页中显示错误消息。这是代码:
//form is sending email through action:"email.php". Code in php file:
mail ($to, $subject, $mess);
//set the error session...(I left out the form validation code to simplify)
$_SESSION["Login.Error"] = "Invalid credentials";
//go back to previous page with all previous selections
$previousPage = $_SERVER['HTTP_REFERER'];
header("Location: $previousPage");
在表单中,我添加了以下代码以显示错误消息:
<p class="form-text text-muted">Error Message:
<?php if(isset($_SESSION['Login.Error'])) {
echo $_SESSION['Login.Error'];
unset($_SESSION['Login.Error']); }
?>
</p>
按下提交后,上一页加载正常,但错误信息没有显示,所以$ _SESSION我猜错了。或者这与$ _SERVER ['HTTP_REFERER']有关吗?有人可以帮忙吗?
答案 0 :(得分:3)
您可以使用session_start();
session_start();
mail ($to, $subject, $mess);
//set the error session...(I left out the form validation code to simplify)
$_SESSION["Login.Error"] = "Invalid credentials";
//go back to previous page with all previous selections
$previousPage = $_SERVER['HTTP_REFERER'];
header("Location: $previousPage")
/ * referer page * /
<?php session_start();?>
<p class="form-text text-muted">Error Message:
<?php if(isset($_SESSION['Login.Error'])) {
echo $_SESSION['Login.Error'];
unset($_SESSION['Login.Error']); }
?>
</p>
答案 1 :(得分:0)
您需要按照以下代码启动会话。
<?php
session_start();
mail ($to, $subject, $mess);
$_SESSION["Login.Error"] = "Invalid credentials";
$previousPage = $_SERVER['HTTP_REFERER'];
header("Location: $previousPage");
?>
在表单中,我添加了以下代码以显示错误消息:
<p class="form-text text-muted">Error Message:
<?php
session_start();
if(isset($_SESSION['Login.Error'])) {
echo $_SESSION['Login.Error'];
unset($_SESSION['Login.Error']);
}
?>
</p>