So I use this same method for other pages, and it works just fine. For example, I use this on my login page, after the user fills in the login form, it calls a php file to query the database. If the query fails, it sets a session variable with an error, and redirects you back to the login page. Where on the login page I have something to check if that session variable is set, if it is, then the error message is displayed.
I'm reusing this same way of showing error messages, for user's who try to sign up, if they have already signed up on this page, then my php file that queries the database should redirect them with the error. Now this same method is working just as it did for my login page. However, it is only displayed after a page refresh, which is not ideal.
Here is the php file that holds the html for users to sign-up, and shows the error message if it's set:
<?php
if(isset($_SESSION['signUpError'])) {
$error = $_SESSION['signUpError'];
unset($_SESSION['signUpError']);
echo '<div class="alert alert-warning alert-dismissible"
role="alert" id="logError" style="width:600px; margin-left:auto; margin-right:auto;">
<span type="button" class="close" data-dismiss="alert" aria-
label="Close"><span aria-hidden="true">×</span></span>
<strong>' . $error . '</strong></div>';
}else{
$error = "";
}
?>
And the php file that get's called after they sign-up:
<?php
session_start();
include("../includes/databaseHandler.inc.php");
$title = $_POST['title'];
$newTitle = $_POST['newTitle'];
$id = $_POST['id'];
$joinName= "%".$_POST['joinName']."%";
if($stmt = mysqli_prepare($conn, "SELECT title FROM rooms WHERE title LIKE ?")){
mysqli_stmt_bind_param($stmt, 's', $joinName);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if($row = mysqli_fetch_assoc($result)){
$_SESSION['signUpError'] = "This Dentist/Hygienist is already assigned to a room!";
header('Refresh:5; url = ../dentist_home.php');
exit;
}
}
if($stmt = mysqli_prepare($conn, "UPDATE rooms SET title = ? WHERE title = ? AND id = ?" )){
mysqli_stmt_bind_param($stmt, "sss", $newTitle, $title, $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
}
mysqli_close($conn);
?>
For the login page, I use header("Location: file_name.php");
but if I use that on this page it doesn't show the error at all, even if the page is refreshed. I'm still kind of new to PHP, so any insight into what I may be doing wrong would be great.