Reputation: 265
I have the following code which will check to see if the following fields produced an error:
//Input Validations
if($user_name == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($user_password == '') {
$errmsg_arr[] = 'Username Password missing';
$errflag = true;
}
if($insp_name == '') {
$errmsg_arr[] = 'Inspector Name missing';
$errflag = true;
}
if($insp_email == '') {
$errmsg_arr[] = 'Inspector Email missing';
$errflag = true;
}
if($confirm_password == '') {
$errmsg_arr[] = 'Confirm Password missing';
$errflag = true;
}
if ($user_password != $confirm_password) {
$errmsg_arr[] = 'The password which you have entered do not match';
$errflag = true;
}
$result = mysql_query("SELECT * FROM members WHERE `email` = '$insp_email' or `login` = '$user_name' LIMIT 1" );
$exist = mysql_fetch_row($result);
if ($exist !==false ) {
$errmsg_arr[] = 'That email is already registered.';
$errflag = true;
}
//If there are input validations, redirect back to the registration form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: accountinfo.php");
exit();
}
If an error was produced once the user is redirected back to the registration page, no error message displays. So the user has no idea why they were redirected back to the registration page. How can I get it so that once they are redirected back to the registration page, it will display an error message stating what the problem was? Ex. Username was missing, or email already registered. I want to display the error message pertaining to the error that caused them to be redirected back to the registration page.
Upvotes: 3
Views: 6361
Reputation: 9056
The most common approach is to use flash messages
. The idea is to store a flash message using session
. If the message is exists in the current session, you show it's contents and delete it (so we are sure it is a "one-time" message):
// Register page code:
...
$_SESSION['flash_message'] = 'An error has occurred on the previous page! You\'ve broken something!';
...
After redirect:
if (!empty($_SESSION['flash_message'])) {
// let's show our message to a user
echo $_SESSION['flash_message'];
// and don't forget to erase it from session
unset($_SESSION['flash_message']);
}
I would suggest to write your own class for this purpose, e.g.:
FlashMessage::set('error', 'the error message itself');
FlashMessage::get('error'); // will perform the unset
// or
FlashMessage::render('template', 'error'); // would render error template and show error message immediatelly if exists... etc.
Upvotes: 5
Reputation: 1
print "You have input errors. These are:";
print "<BR><UL>";
for ($i=0; $i<sizeof($errmsg_arr); $i++)
{
print "<LI>$errmsg_arr[$i]";
}
insert below of your code and it will show what field/s are missing.
Upvotes: 0
Reputation: 284836
Add a error_message
or error_messages
GET parameter to accountinfo.php.
Then, if there is an error, redirect to:
header("location: accountinfo.php?error_message=$some_message");
I note that there can be more than one error, so you need to decide if you want to display them all at once.
An alternative solution is to store the list of errors in the session
:
$_SESSION['registration_errors'] = $errmsg_arr;
Also note that your code is vulnerable to SQL injection.
Upvotes: 0