首頁  >  問答  >  主體

如何在同一頁面提交表單後顯示訊息

<?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>

我透過同一頁上的 post 方法提交表單。提交表單後,我檢查一個條件,如果使用者名為空,則顯示 $error1 = "請輸入使用者名稱";表單頂部的訊息。我面臨的問題是當我刪除 header("location: index.php"); 時訊息正確顯示在正文部分。但如果新增 header("location:index.php");用於重新整理頁面 $error1 由於頁面重新整理而未顯示。但沒有標題(「位置:index.php」);瀏覽器在刷新頁面期間給出的訊息是(要顯示此頁面,Firefox Developer Edition 必須發送將重複之前執行的任何操作(例如搜尋或訂單確認)的資訊。)如何正確顯示訊息任何建議以及如何防止提交表單後重複操作,包括標題(“位置:index.php”);新增。 在此輸入代碼

P粉155128211P粉155128211316 天前406

全部回覆(1)我來回復

  • P粉513318114

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

    在會話中新增錯誤並檢查會話設定值是否回顯錯誤並取消設定錯誤會話

    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");
        }
            
            
    }
    ?>
    
                        
    
    
       

    回覆
    0
  • 取消回覆