php - 重定向到另一个页面并显示警告消息错误
问题描述
我在模式中创建了一个登录表单,用户将输入他的用户名密码和他的目的,成功后直接进入主页,现在我的问题是我创建了另一个页面,如果用户输入错误的用户名或密码,他将被重定向到表单上方的另一个页面 我想显示错误警报消息,用户名或密码不正确。你能帮我解决这个问题吗?
提前谢谢。
这是将显示错误消息的示例表单:
<form class="needs-validation " action="#" method="post" novalidate >
<h3 class="dark-grey-text text-center">
<strong><i class="fa fa-user"></i> Login</strong>
</h3>
<hr>
<span id="error_message" class="alert alert-danger"></span>
<div class="row" style="margin-left:300px;">
<label>Date:</label>
<span id="date" name="date"></span>
</div>
<div class="row" style="margin-left:300px;">
<label>Time:</label>
<span id="clock" name="time"></span>
</div>
<div class="md-form form-sm mb-5" style="margin-top:05px;">
<i class="fa fa-user prefix"></i>
<input type="text" id="username3" name="username3" class="form-control form-control-sm " required>
<label for="modalUsername">Username</label>
<div class="invalid-feedback" style="margin-left:30px">
Enter username.
</div>
</div>
</form>
这是我的php代码:
<?php
include_once ('connection.php');
if(isset($_POST['submit']))
{
$username1 = $_POST['username1'];
$password2= $_POST['password2'];
$purpose =$_POST["purpose"];
$date=date("Y-m-d");
$time=date("H:i:s");
$sql = "select * from visitor where uname = '$username1' and pass = '$password2'";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
$('#error_message').html("Incorrect username or password");
header("Location:login.php");
} else {
while ($row = mysqli_fetch_array($result)) {
$username1 = $row['uname'];
$password2 = $row['pass'];
$fname=$row['fname'];
$lname=$row['lname'];
$InsertSql = "INSERT INTO visitor_att(fname,lname,uname,pass,purpose,date,timein) VALUES ('$lname','$fname',
'$username1','$password2','$purpose','$date','$time')";
$res = mysqli_query($conn, $InsertSql);
header("Location:test.php?");
}
}
}
?>
解决方案
这不是 PHP 的工作方式 :)
在我看来,你能做的最好的事情是:
- 在使用 header 函数之前,向 $_SESSION 添加新键,可以调用
messages
它,在其中添加消息数组。 - 将 .html 表单转换为 php 文件(也将打印表单)。
- 在表单上方,在
<?php ?>
标签内,检查$_SESSION['messages']
. 如果有,则显示引导警报 HTML 标记。
IE:
save.php 将是一个稍微修改的 php 文件
<?php
include_once ('connection.php');
if(isset($_POST['submit'])) {
$username1 = $_POST['username1'];
$password2 = $_POST['password2'];
$purpose = $_POST["purpose"];
$date = date("Y-m-d");
$time = date("H:i:s");
$sql = "select * from visitor where uname = '$username1' and pass = '$password2'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
$_SESSION['error'] = 'Incorrect username or password';
header("Location:login.php");
} else {
while ($row = mysqli_fetch_array($result)) {
$username1 = $row['uname'];
$password2 = $row['pass'];
$fname = $row['fname'];
$lname = $row['lname'];
$InsertSql = "INSERT INTO visitor_att(fname,lname,uname,pass,purpose,date,timein) VALUES ('$lname','$fname',
'$username1','$password2','$purpose','$date','$time')";
$res = mysqli_query($conn, $InsertSql);
header("Location:test.php?");
}
}
}
和 form.php 将是:
<?php
if(!empty($_SESSION['error'])){
?>
<div id="error_message"><?php echo $_SESSION['error'] ?></div>
<?php
unset($_SESSION['error']);
}
?>
<form class="needs-validation " action="save.php" method="post" novalidate >
<h3 class="dark-grey-text text-center">
<strong><i class="fa fa-user"></i> Login</strong>
</h3>
<hr>
<span id="error_message" class="alert alert-danger"></span>
<div class="row" style="margin-left:300px;">
<label>Date:</label>
<span id="date" name="date"></span> </div>
<div class="row" style="margin-left:300px;">
<label>Time:</label>
<span id="clock" name="time"></span> </div>
<div class="md-form form-sm mb-5" style="margin-top:05px;">
<i class="fa fa-user prefix"></i>
<input type="text" id="username3" name="username3" class="form-control form-control-sm " required>
<label for="modalUsername">Username</label>
<div class="invalid-feedback" style="margin-left:30px">
Enter username. </div>
</div>
</form>
推荐阅读
- html - 使标题 (h1, h2, ...) 从正文继承样式
- php - 如何阻止 laravel 用户在设备之间交换
- c# - 尝试使用 LINQ to SQL 创建节点树会产生 NotSupportedException
- c++ - 使用 __single_client_alloc 找到了一些旧代码 - 我该如何恢复它?
- c# - 如何实现具有公共属性但私有设置方法的多个接口?
- python - 从附近目录而不是从站点包中导入 Python 模块
- ios - iOS Swift:切换到耳机或从耳机切换时录音机中断
- python - 用标签替换字符串中的单词
- c# - 如何在 Linux 上的 .net 核心中获取系统规格
- mysql - Wordpress php_network_getaddresses: getaddrinfo failed: Temporary failure ,但其他与 mysql 连接的尝试工作正常