title: Displaying PHP login error on the same page?
body: <p>I have two pages. Index.php and my Login.php.</p>
  
  <p>I am using dropdown menu form for logging in - when the user presses 'Sign in', the data gets sent to the Login.php. It's all okay when the password/username was correct (they get sent to the dashboard).</p>
  
  <p>But how could I display an error right there, in the form? Currently when the username/password is wrong it sends you to a new blank page. I want the error to appear right there on the form.</p>
  
  <p>My form: </p>
  
  <pre><code>&lt;div class="form-group"&gt;
     &lt;label class="sr-only" for="username"&gt;Username&lt;/label&gt;
     &lt;input type="text" class="form-control" name="user_name" placeholder="Username" required&gt;
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
     &lt;label class="sr-only" for="password"&gt;Password&lt;/label&gt;
     &lt;input type="password" class="form-control" name="user_password" placeholder="Password" required&gt;
  &lt;/div&gt;
  &lt;div class="checkbox"&gt;
     &lt;label&gt;
     &lt;input type="checkbox"&gt; Remember me
     &lt;/label&gt;
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
     &lt;button type="submit" class="btn btn-success btn-block"&gt;Sign in&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My login.php:</p>
  
  <pre><code>error_reporting(E_ALL);
  
  session_start();
  $con = mysqli_connect("localhost", "root", "", "battlesql");
  
  if (!$con) {
      echo "&lt;div&gt;";
      echo "Failed to connect to MySQL: ".mysqli_connect_error();
      echo "&lt;/div&gt;";
  } else {
      if (isset($_POST['user_name']) &amp;&amp; isset($_POST['user_password'])) {
  
          $username = stripslashes($username);
          $username = mysqli_real_escape_string($con, $_POST['user_name']);
  
          $pass = stripslashes($pass);
          $pass = mysqli_real_escape_string($con, $_POST['user_password']);
  
          $pass_hashed = hash('whirlpool', $pass);
  
          $select_user = "SELECT * from accounts where name='$username' AND password='$pass_hashed' LIMIT 1";
          $query = mysqli_query($con, $select_user);
  
          $row = mysqli_num_rows($query);
  
          if (!$row) {
              echo "&lt;div&gt;";
              echo "No existing user or wrong password.";
              echo "&lt;/div&gt;";
          } else {
              echo "&lt;div&gt;";
              echo "You have been logged in.";
              echo "&lt;/div&gt;";
          }
  
      } else {
          echo "MySQL error!";
      }
  }
  </code></pre>