如何在同一页面提交表单后显示消息
P粉155128211
P粉155128211 2023-12-31 16:09:22
0
1
429

<?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粉155128211
P粉155128211

全部回复(1)
P粉513318114

在会话中添加错误并检查会话设置值是否回显错误并取消设置错误会话

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

                    


   

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板