We have a login page which validates the username and password from our database, however we'd like it to display the invalid username and password message above the input fields rather than at the top of the page. We know we can edit and put the php code in the div to display it where it is, we put out headers and therefore as soon as we move the php from the top of the page it generates an error message. Is there an alternative to this? Or a way of using the headers differently? Our code is below and an image of what we want...
<?php
error_reporting (E_ALL ^ E_NOTICE);
$username = $_POST['usrname'];
$pass_word = $_POST['pass_word'];
if(($usrname=="")&&($pass_word==""))
{
//Do nothing as nothing has been posted......
}
else
{
$con = mysql_connect("localhost"," "," ");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
$result = mysql_query("SELECT * FROM `leicester_login` WHERE `user_name`='$username'", $con);
$num_row = mysql_num_rows($result);
}
if($username=="")
{
//do nothing as nothing has been posted
}
else
{
if($num_row == 0)
{
echo "<p>The username <b>". $username ."</b> doesnt exist!</p>";
}
else
{
$return_pass = mysql_fetch_array($result);
if($pass_word == $return_pass['pass_word'])
{
session_start();
if(isset($_SESSION['username']))
{
//do nothing as session already exists...
header("Location: ");
}
else
{
$_SESSION['username']= $username;
header("Location: ");
exit;
}
}
else
{
echo "<p>The password entered is incorrect!</p>";
}
}
}
?>
<html lang="en">
<head>
</head>
<body>
<div class="container">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" class="form-signin">
<h2 class="form-signin-heading"><img src="" alt="Logo"/>Leicester</h2>
<?php
?>
<h6 class="form-signin-heading">You are not logged in, please login.</h6>
<input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">
<input type="password" name="pass_word" id="pass_word" class="input-block-level" placeholder="Password">
<center><button align="center" class="btn btn-large btn-primary" type="submit">Sign in</button></center>
<a href="shires.php" class="btn btn-link">< Go Back</a>
</form>
</div> <!-- /container -->
</body>
</html>