简体   繁体   English

如何在同一页面上提交表单后显示消息

[英]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.我通过同一页面上的 post 方法提交表单。 after submitting the form i check one condition if username is null then show $error1 = "Please enter username";提交表单后,我检查一个条件,如果用户名为空,然后显示 $error1 = "请输入用户名"; message on top of the form.表单顶部的消息。 the problem i face is when i remove header("location: index.php");我面临的问题是当我删除 header("location: index.php"); message shown properly on body section.消息在正文部分正确显示。 but if add header("location: index.php");但是如果添加 header("location: index.php"); for refreshing page $error1 not displaying because of page refresh.用于刷新页面 $error1 由于页面刷新而未显示。 but without header("location: index.php");但没有标题(“位置: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");浏览器在刷新页面时给出消息是(要显示此页面,Firefox 开发者版必须发送重复之前执行的任何操作(例如搜索或订单确认)的信息。)如何正确显示消息任何建议以及如何防止提交表单后重复操作,包括 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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