Whats the best way to display user-friendly error messages when username or password is empty for example or failed to login. Is there a way to set textbox with specific text when error apears?
Index.php file. Has a textbox called "Error" and i want to display error msg when error happens into the textbox.
<?php
include 'login.php';
?>
<html>
<head>
<title>Form site</title>
<link rel="stylesheet" type ="text/css" href="style.css">
</head>
<body>
<img src="imge/logo.png" id="logo" width="270" height="130">
<div id ="frm">
<form method="post" action="login.php">
<p>
Username : <input type="text" name="username" id="username" ><br><br>
Password : <input type="password" name="password" id="password"><br><br>
<input type="submit" value="Login" id="btn" name="submit">
<input type="text" value="Error" id="Error" name="Error">
</p>
</div>
</form>
</body>
</html>
Login.php file. Where i handle the errors. Is there a way when error happens to echo it to the textbox? At the moment i have no clue how to handle and inform user about these errors. Any suggestions will be appreciated! :)
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST['username']) || empty($_POST['password'])){
header("Location: index.php");
// FIRST ERROR
}
else
{
//Define $user and $pass
$user=$_POST['username'];
$pass=$_POST['password'];
//Establishing Connection with server by passing server_name, user_id and pass as a patameter
$conn = mysqli_connect("localhost", "root", "");
//Selecting Database
$db = mysqli_select_db($conn, "loginsystem");
//sql query to fetch information of registerd user and finds user match.
$query = mysqli_query($conn, "SELECT * FROM users WHERE Password ='$pass' AND Username ='$user'");
$rows = mysqli_num_rows($query);
if($rows == 1){
header("Location: welcome.php"); // Redirecting to other page
}
else
{
header("Location: index.php");
//SECOND ERROR
}
mysqli_close($conn); // Closing connection
}
}
?>