[英]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>
我通过同一页面上的 post 方法提交表单。 提交表单后,我检查一个条件,如果用户名为空,然后显示 $error1 = "请输入用户名"; 表单顶部的消息。 我面临的问题是当我删除 header("location: index.php"); 消息在正文部分正确显示。 但是如果添加 header("location: index.php"); 用于刷新页面 $error1 由于页面刷新而未显示。 但没有标题(“位置:index.php”); 浏览器在刷新页面时给出消息是(要显示此页面,Firefox 开发者版必须发送重复之前执行的任何操作(例如搜索或订单确认)的信息。)如何正确显示消息任何建议以及如何防止提交表单后重复操作,包括 header("location: index.php"); 添加。 enter code here
在会话中添加错误并检查会话是否设置值回显错误和未设置错误会话
<?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.