title: PHP user friendly error handling
body: <p>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?</p>
  
  <p>Index.php file. Has a textbox called "Error" and i want to display error msg when error happens into the textbox.</p>
  
  <pre><code>      &lt;?php
           include 'login.php';
        ?&gt;
      &lt;html&gt;
        &lt;head&gt;
        &lt;title&gt;Form site&lt;/title&gt;
       &lt;link rel="stylesheet" type ="text/css" href="style.css"&gt;
       &lt;/head&gt;
       &lt;body&gt;
       &lt;img src="imge/logo.png" id="logo"  width="270" height="130"&gt;
  
       &lt;div id ="frm"&gt;
       &lt;form method="post" action="login.php"&gt;
        &lt;p&gt;
          Username : &lt;input type="text" name="username" id="username" &gt;&lt;br&gt;&lt;br&gt;
          Password : &lt;input type="password" name="password" id="password"&gt;&lt;br&gt;&lt;br&gt;
          &lt;input type="submit" value="Login" id="btn" name="submit"&gt;
          &lt;input type="text" value="Error" id="Error" name="Error"&gt;
    &lt;/p&gt;
     &lt;/div&gt;
   &lt;/form&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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! :)</p>
  
  <pre><code>&lt;?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
  }
  }
   ?&gt;
  </code></pre>