[英]How can I display the error message after submitting the form in php?
I have a webpage which displays content after a selection and it contains a form with a submit button. 我有一个网页,在选择后显示内容,它包含一个带有提交按钮的表单。 Once the submit button is clicked I want to display the error messages in the previous page.
单击提交按钮后,我想在上一页中显示错误消息。 Here is the code:
这是代码:
//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");
In the form I added the following code to display the error message: 在表单中,我添加了以下代码以显示错误消息:
<p class="form-text text-muted">Error Message:
<?php if(isset($_SESSION['Login.Error'])) {
echo $_SESSION['Login.Error'];
unset($_SESSION['Login.Error']); }
?>
</p>
After pressing submit the previous page is loading OK, but the error message is not displaying, so there is something wrong with the $_SESSION I guess. 按下提交后,上一页正在加载OK,但错误信息没有显示,所以$ _SESSION我猜错了。 Or does this have to do with $_SERVER['HTTP_REFERER'];?
或者这与$ _SERVER ['HTTP_REFERER']有关吗? Can somebody help?
有人可以帮忙吗?
You can use session_start(); 你可以使用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 */ / * 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>
You need to start session as per below code. 您需要按照以下代码启动会话。
<?php
session_start();
mail ($to, $subject, $mess);
$_SESSION["Login.Error"] = "Invalid credentials";
$previousPage = $_SERVER['HTTP_REFERER'];
header("Location: $previousPage");
?>
In the form I added the following code to display the error message: 在表单中,我添加了以下代码以显示错误消息:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.