Home  >  Q&A  >  body text

How to display message after form submission on the same page

<?php
session_start();
include("includes/config.php"); 

if(isset($_POST['submit'])){
    
    if (empty($username)){
        $error1 = "Please enter username.";
        header("location: index.php");

    }
    
    $username=mysqli_real_escape_string($con,$_POST['username']);
    $password=mysqli_real_escape_string($con,$_POST['password']);
    
    $sql="select * from admin_user where username='$username' and password='$password'";
    
    $res=mysqli_query($con,$sql);
    if (mysqli_num_rows($res)>0){
        
       session_start();
       $_SESSION["username"] = $username;
       $_SESSION["id"] = $id;
       $_SESSION['admin']['status']= true ;

      header("location: dashboard.php");
    }else{
        $unsucess = "Invalid username or password.";
header("location: index.php");
    }
        
        
}
?>

                    

<?php if(!empty($error1)) {?>
   <p><?php echo $error1; ?></p>
<?php } ?>
                       
                           <form method ="post">
                              <div class="mb-3">
                                 <label for="username" class="form-label">Username*</label>
                                 <input type="text" name="username" class="form-control" id="username" placeholder="Enter username">
                              </div>
                              <div class="mb-3">
                           
                                 <label class="form-label" for="password-input">Password*</label>
                                 <div class="position-relative auth-pass-inputgroup mb-3">
                                    <input type="password" name="password" class="form-control pe-5" placeholder="Enter password" id="password">
                                    <button class="btn btn-link position-absolute end-0 top-0 text-decoration-none text-muted shadow-none" type="button" id="password-addon"><i class="ri-eye-fill align-middle"></i></button>
                                 </div>
                              </div>
                              <div class="mt-4">
                                 <button class="btn btn-success w-100" name="submit" type="submit">Sign In</button>
                              </div>
                   
                           </form>

I submit the form via the post method on the same page. After submitting the form, I check a condition and if the username is empty, I display $error1 = "Please enter username"; message at the top of the form. The problem I am facing is when I remove header("location: index.php"); the message is displayed correctly in the body section. But if you add header("location:index.php"); for refreshing the page $error1 is not displayed due to page refresh. but there is no title ("Location: index.php"); the message the browser gives during page refresh is (To display this page, Firefox Developer Edition must send a Info.) Any suggestions on how to display the message correctly and how to prevent duplication after submitting the form, including header("location: index.php"); added. Enter code here

P粉155128211P粉155128211267 days ago382

reply all(1)I'll reply

  • P粉513318114

    P粉5133181142024-01-01 14:35:37

    Add an error in the session and check if the session setting value echoes the error and unset the error session

    0){
            
           session_start();
           $_SESSION["username"] = $username;
           $_SESSION["id"] = $id;
           $_SESSION['admin']['status']= true ;
    
          header("location: dashboard.php");
        }else{
            $_SESSION['error1'] = "Invalid username or password.";
            header("location: index.php");
        }
            
            
    }
    ?>
    
                        
    
    
       

    reply
    0
  • Cancelreply