简体   繁体   中英

How to display message after form submit on 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 form via post method on same page. after submitting the form i check one condition if username is null then show $error1 = "Please enter username"; message on top of the form. the problem i face is when i remove header("location: index.php"); message shown properly on body section. but if add header("location: index.php"); for refreshing page $error1 not displaying because of page refresh. but without header("location: index.php"); browser give message during refresh page is (To display this page, Firefox Developer Edition must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.) how to show message properly any suggestion and how to prevent repeat action after submitting form including header("location: index.php"); added. enter code here

add error in session and check if session isset value echo error and unset error session

 <?php session_start(); include("includes/config.php"); if(isset($_POST['submit'])){ if (empty($username)){ $_SESSION['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{ $_SESSION['error1'] = "Invalid username or password."; header("location: index.php"); } } ?> <?php if(isset($_SESSION['error1'])) {?> <p><?php echo $_SESSION['error1']; ?></p> <?php unset($_SESSION['error1']); } ?> <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM