簡體   English   中英

如何顯示登錄表單無效的登錄憑據的警報消息

[英]How to display alert message for the invalid login credentials for the login form

<?php include('includes/config.php');
if(isset($_POST["submit"])){
$empid=$_POST["empid"];
$pass=$_POST["password"];

$query=mysqli_query($conn,"SELECT employee_id, fname,lname,empid,password, 
status, role FROM employee where empid='$empid' and password='$pass'");
$row=mysqli_fetch_array($query);
if(is_array($row)) 
{

        session_start();
        $_SESSION["empid"]=$empid;
        $_SESSION["role"]=$row["role"];
        $_SESSION["eid"]=$row["empid"];        
        $_SESSION["status"]=$row['status'];
        $_SESSION["employee_id"]=$row['employee_id'];
        $_SESSION['uname']=$row['fname']." ".$row['lname'];
        if($_SESSION["role"]=='admin' && $_SESSION["status"]>0){
            $_SESSION['alogin']=$_POST['empid'];
                header("Location:admin/home.php");          
        }
        elseif($_SESSION["role"]=='TL' && $_SESSION["status"]>0){
            $_SESSION['tlogin']=$_POST['empid'];
            header("Location:TL/home.php");
        }
        else{
            $_SESSION['emplogin']=$_POST['empid'];
            header("Location:home.php");
        }


}
 else{
    echo "<script>alert('Invalid login details');</script>" // this is not working
     header("Location:index.php");
        }
   }
  ?>

我已經創建了一個包含員工ID和密碼的登錄表單。 如果憑據錯誤,我想在代碼中將警報消息顯示為無效的登錄憑據,但它不起作用。 由於腳本包含在php代碼之間。 請幫助我如何獲得警報錯誤消息。

回顯HTML 重定向將不起作用。 我的建議是傳遞一個參數,像這樣:

header("Location:index.php?error=login");

您可以在index.php中閱讀該內容,並相應地顯示錯誤:

$err = filter_input(INPUT_GET, 'error');
if ($err === "login") echo "<script>alert('Invalid login!');</script>";

您可以通過在重定向到index.php時添加GET參數來對它進行警報...

喜歡:

index.php?invalid_login=true

這行:

 header("Location:index.php");

應該:

 header("Location:index.php?invalid_login=true");

然后在index.php中添加以下代碼。

if( isset( $_GET['invalid_login'] ) AND $_GET['invalid_login'] == 'true' ) {
     echo "<script>alert('Invalid login details');</script>";

}

if(mysqli_num_rows($row) > 0)替換if(is_array($row)) if(mysqli_num_rows($row) > 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM