/    Sign up×
Community /Pin to ProfileBookmark

PHP Login Page : Display error message within FIELDSET (or above)

I have a Login-page designed (in PHP) and working fine.

If the user’s credentials are verified, he is re-directed to the website’s main-page.

However, if he inputs the wrong credentials (username and/or password), he gets an error message saying : “the username or password is incorrect. Please try again”……….or some such thing.

This is also working fine…….except the styling. When my php codes “echoes” the error message, it is displayed at [B]the very top, left corner of the screen. [/B]

I would like to display the error message either within the log-in box itself, or at least directly above the log-in box.

Here is the PHP code for the login process :

[code=php]if(!empty($_POST[‘login’])) {

$query = mysqli_query($conn,(“SELECT * FROM my_table where login = ‘$_POST[login]’ AND password = ‘$_POST[password]'”))
or die(mysqli_error($conn));

$row = mysqli_fetch_array($query);

if(!empty($row[‘login’]) AND !empty($row[‘password’])) {
$_SESSION[‘login’] = $row[‘login’];
$_SESSION[‘password’] = $row[‘password’];

echo “LOGIN SUCCESSFULL. Re-directing……………”; }

else {
echo “Sorry. Wrong username and/or password.”;
}
}

?>
[/code]

And here is my log-in box (HTML) :

[code=html]
<body>
<div id=”Sign-In”>
<fieldset style=”width:30px”><legend><b>Please log in :</b></legend>
<form method=”POST” action=”login.php”>
Username <br><input type=”text” name=”login” size=”40″ required><br>
Password <br><input type=”password” name=”password” size=”40″ required>
<br>
<br>
<input id=”button” type=”submit” name=”submit” value=”Log In”>
<input id=”button” value=”Cancel” onclick=”location.href=’xxxxxxxxxxxxx'”>
<br>
</form>
</fieldset>
</div>
</body>
[/code]

When login is successful, the user is re-directed to another page;

If not, that error message is displayed : [B]echo “Sorry. Wrong username and/or password.”;[/B]

However, I would like the message displayed in alignment with the login-box itself

I’ve tried using all forms of CSS styling, but the message keeps getting displayed “[B]outside[/B]” the realm of the login “fieldset box”.

The only other option I can think of is to re-direct the user to another page if login is unsuccessful. But, this is a nuisance for the user.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@LandslydeMar 03.2015 — Let me show you a couple lines of my HTML and a few from the corresponding PHP.

[code=html]
<p>
<label for="zip" class="label">zipcode:</label><br>
<input type="text" name="zip" placeholder="Zipcode" value="<?php if (isset($_POST['zip'])) { echo $zip; } ?>"><br>
<span class="error"> <?php echo $zipErr;?></span>
</p><br>[/code]


[code=php]
if (empty($_POST["zip"])) {
$zipErr = "* Zipcode is required";
} else {
$zip = test_input($_POST["zip"]);
if (!preg_match("/^[0-9]*$/",$zip)) {
$zipErr = "* Numeric only";
}
}[/code]


When $zipErr triggers, it goers directly underneath the input box. Hope this helps.
Copy linkTweet thisAlerts:
@NogDogMar 03.2015 — One approach is to put errors into a variable (most likely an array element) that you can look for in your HTML where needed. So let's say the error is that the user did not supply a "user_name" value...
[code=php]
$errors['user_name'] = "You must supply a User Name.";
[/code]

In your HTML...
[code=php]
<div>
<label for="user_name">User Name:</label>
<input type="text" name="user_name" />
<?php if(!empty($errors['user_name'])): ?>
<span class="form_error"><?= $errors['user_name'] ?></span>
<?php endif; ?>
</div>
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 03.2015 — PS: If you are going to do a HTTP redirect back to the form instead of including it (I prefer the latter over redirects), then you would need to user $_SESSION['errors'] instead of $errors, and make sure you do session_start() on each page, and then clear out $_SESSION['errors'] after using it -- which is one of the main reasons I don't like redirecting. ?
Copy linkTweet thisAlerts:
@newuserphpauthorMar 03.2015 — PS: If you are going to do a HTTP redirect back to the form instead of including it (I prefer the latter over redirects), then you would need to user $_SESSION['errors'] instead of $errors, and make sure you do session_start() on each page, and then clear out $_SESSION['errors'] after using it -- which is one of the main reasons I don't like redirecting. ?[/QUOTE]

I also prefer to include. It makes no sense to re-direct. This only confuses and annoys the user ?
Copy linkTweet thisAlerts:
@newuserphpauthorMar 03.2015 — One approach is to put errors into a variable (most likely an array element) that you can look for in your HTML where needed. So let's say the error is that the user did not supply a "user_name" value...
[code=php]
$errors['user_name'] = "You must supply a User Name.";
[/code]

In your HTML...
[code=php]
<div>
<label for="user_name">User Name:</label>
<input type="text" name="user_name" />
<?php if(!empty($errors['user_name'])): ?>
<span class="form_error"><?= $errors['user_name'] ?></span>
<?php endif; ?>
</div>
[/code]
[/QUOTE]



This is almost similar to the earlier response.

Perhaps, my mistake is : not placing the error-variable in the correct place inside my PHP code

Where exactly should I place it, so I dont get the error : "[B]Undefined Variable"[/B] ?

Thanks
Copy linkTweet thisAlerts:
@NogDogMar 03.2015 — Not sure what you're referring to, as I only see where you echo out a string literal -- you don't assign anything to a variable?

To follow my approach, where you echo out the error message, instead assign the error to a variable, and then look for that variable (e.g. using !empty() ) like in my example right where you would actually want the error message displayed if there is an error.
Copy linkTweet thisAlerts:
@newuserphpauthorMar 03.2015 — Not sure what you're referring to, as I only see where you echo out a string literal -- you don't assign anything to a variable?

To follow my approach, where you echo out the error message, instead assign the error to a variable, and then look for that variable (e.g. using !empty() ) like in my example right where you would actually want the error message displayed if there is an error.[/QUOTE]



It worked now ! Thanks ?
×

Success!

Help @newuserphp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...