Click here to Skip to main content
15,746,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am wondering how to get login error messages on the same page. As of right now I am only able to get to show with opening a new page and echoing "Invalid username or password". If you can help I'd love to hear your ideas thank you so much!

loginpage.php code below
<!DOCTYPE html>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>L&B Furniture</title>
    <link rel="stylesheet" href="style.css" />
    <link rel="preconnect" href="" />
    <link rel="preconnect" href="" crossorigin />
  <body onload="slider()">
    <div class="back">
      <div class="slider">
        <img src="images/bedroom.jpeg" alt="" id="slideImg" />
      <div class="overlay">
        <div class="nav-design">
          <div class="logo">L&B FURNITURE</div>
        <div class="content">
          <h1>L&B is all you need!</h1>
            All customers are required to create an account before they are able
            to continue to the homepage
          <h3>Do you have an account already? Please login in below:</h3>
            <form action="login.form.php" method="POST">
              <label class="usernamestyle">User Name :</label
                style="margin-bottom: 100px"
                placeholder="Enter User Name"
              /><br />
              <label class="passwordusername">Password: </label
                placeholder="Enter Password"
              <button class="loginbuttons" type="submit">Login</button>
            <form action="register.form.php" method="POST">
              <button class="registerbutton" type="Submit">
                Create an account
      var slideImg = document.getElementById("slideImg");

      var images = new Array(

      var len = images.length;
      var i = 0;

      function slider() {
        if (i > len - 1) {
          i = 0;
        slideImg.src = images[i];
        setTimeout("slider()", 4000);

login.form.php which is called from the main login page.



$usersUSERSNAME = $_POST['user'];  
$usersPWD = $_POST['pwd'];  
    //to prevent from mysqli injection  
    $username = stripcslashes($usersUSERSNAME);  
    $password = stripcslashes($usersPWD);  
    $username = mysqli_real_escape_string($conn, $usersUSERSNAME);  
    $password = mysqli_real_escape_string($conn, $usersPWD);  
    $sql = "select *from users where usersUSERSNAME = '$usersUSERSNAME' and usersPWD = '$usersPWD'";  
    $result = mysqli_query($conn, $sql);  
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);  
    $count = mysqli_num_rows($result);  
    if($count == 1){  
        header("location: homepage.php");
        echo "<h1> Login failed. Invalid username or password.</h1>";  

What I have tried:

I havent tried much, I tried to play around with the $_SESSION and create an error but I couldnt get it to work
Updated 2-Dec-21 4:37am
Richard Deeming 2-Dec-21 11:47am    
Also, never store passwords in plain text.
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP even has built-in functions to help you do the right thing.
PHP: password_hash[^]
PHP: password_verify[^]

1 solution

The main issue comes from how you've named your form elements vs. what you're expecting in PHP:

<input .. name="usersUSERSNAME" .. />
<input .. name="usersPWD" .. />

$usersUSERSNAME = $_POST['user'];  
$usersPWD = $_POST['pwd'];

You're trying to reference inputs with different names. Remember that the $_POST variable contains values corresponding to the names of the elements in your form. So in this case your 'user' needs to be 'usersUSERSNAME' and your 'pwd' needs to be 'usersPWD' so that they match. Or you can choose to rename the input elements instead.

That being said, obligatory comment about using variables directly within an SQL statement. Even using escape string it's still better practise to use prepared statements and bind the parameters. PHP MySQL Prepared Statements[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900