重定向到另一个页面并显示警告消息错误

时间:2018-07-30 08:32:30

标签: php html bootstrap-4

我在模式内创建了一个登录表单,用户将输入其用户名 通过和他的目的,成功后直接转到主页,现在 我的问题是我创建了另一个页面,如果用户键入了错误的用户名或 密码,他将被重定向到我要显示的表单上方的另一页 错误警报消息,错误的用户名或密码。你能帮我做这个吗?

提前谢谢。

以下是显示错误消息的示例表单:

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

1 个答案:

答案 0 :(得分:3)

这不是PHP的工作方式:)

我认为您能做的最好的是:

  • 在使用标题函数之前,将新密钥添加到$ _SESSION,它可能被称为select owner, '{{' || group_concat( event||','||parent||','||key||','||target||','||tick_count||','||tick_interval||',' ||ifnull(arg1,'NULL')||','||ifnull(arg2,'NULL')||','||ifnull(arg3,'NULL')||','||ifnull(arg4,'NULL')||',' ||ifnull(arg5,'NULL')||','||ifnull(arg6,'NULL')||','||ifnull(arg7,'NULL')||','||ifnull(arg8,'NULL')||',' ||ifnull(arg9,'NULL')||','||ifnull(arg10,'NULL')||','||ifnull(arg11,'NULL')||','||ifnull(arg12,'NULL')||',' ||ifnull(arg13,'NULL')||','||ifnull(arg14,'NULL')||','||ifnull(arg15,'NULL')||','||hasReq, '},{')||'}}' as key_value_pairs from items_functions WHERE owner = 281201 group by owner ,并在其中添加消息数组。
  • 将.html表单转换为php文件(也将仅打印表单)。
  • 在表单上的messages标记内,检查<?php ?>中是否有内容。如果存在,请显示引导警报HTML标签。

即:

save.php将是经过稍微修改的php文件

$_SESSION['messages']

和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) {

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