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. Or does this have to do with $_SERVER['HTTP_REFERER'];? Can somebody help?
You can use 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>
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.