I have a login.php (input form) and loginvalidation.php(server side validations).
I want to display an error message on login.php when input is invalid. I am not able to redirect user to login.php and display error message on it.
On Login.php the error message field is <label id="lblErrorMessage" style="color: red;"><?=$errorString?></label>.
-
Do you have any code that you've tried so far? In general, you may want to consider using a $_SESSION variable for the error message, here... if you are setting the error message and then redirecting to a new page. (See: php.net/manual/en/reserved.variables.session.php)summea– summea2012-03-03 23:23:54 +00:00Commented Mar 3, 2012 at 23:23
-
I'm not at all sure what your question or your problem is.GolezTrol– GolezTrol2012-03-03 23:25:01 +00:00Commented Mar 3, 2012 at 23:25
Add a comment
|
2 Answers
I presume you have a form in login.php with
<form method="post" action="loginvalidation.php">
In loginvalidation.php add
if ($badLogin) header('location:login.php?err=badLogin');
And catch the err in login.php
$err = $_GET['err'];
I'd recommend to put the validation and form in the same php-page.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//validate and if ok maby redirect (application dependent)
//if no redirect - we still need to login
$err = 'no good';
?>
<html>the login form...
regards, /t
3 Comments
user1247412
What do you mean by to put the validation and form in the same php-page? You mean to say I should include checking for valid login details in login.php itself?
user1247412
can you please elaborate it a bit?
Teson
Yes, exactly. It's more conveniant to keep track of the code that way. There are circumstances when the opposite is more suiting, but make use of the fact that php can live without separate models/objects.
What I do for login forms -
- I set the action to (in your case) login.php,
- do error checking
- a. display the errors if there are any - or -
- b. If there aren't any errors I then redirect to the main page.
2 Comments
user1247412
In my case how will i display error message on login.php (say invalid username/password) after validations are done on loginvalidation.php
cjtech
See user247245's answer below