使用 sweetalert2 草拟登录表单,但它只重定向到管理页面而不是 hello 页面

问题描述 投票:0回答:0

我正在测试 sweetalert2,其中我的登录表单将使用它的设计。但是当我从我的数据库中输入没有“管理员”名称的用户名和密码时,它仍然会重定向到管理页面。

我尝试了一个变体,其中我使用:

header("location: admin.html"); 

重定向我的页面。它工作得非常好,但我需要在我的工作中集成 sweetalert2。 但是我测试过 sweetalert2,它只会重定向到管理页面而不是 hello 页面。

<?php

$host="localhost";
$user="root";
$password="";
$db="collector";
session_start();
$data=mysqli_connect($host, $user, $password, $db);
if ($data === false) {
    die("connection error");
}
if ($_SERVER["REQUEST_METHOD"]=="POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $login = "SELECT * from user where username='".$username."' and password='".$password."' ";
    $result = mysqli_query($data, $login);
    $column = mysqli_fetch_array($result);
    $row = mysqli_num_rows($result);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/11.4.24/sweetalert2.all.js"></script>
</head>
<body>
    <form action="" method="post">
    <h1>Login</h1>
    <input type="text" name="username" placeholder="Email" >
    <input type="text" name="password" placeholder="Password" >
    <input type="submit" name="sub" value="Login">

    <?php
    if(isset($_POST['sub'])){
        $n=$_POST['username'];
        $e=$_POST['password'];
        if(empty($n) || empty($e)){
        ?>
        <script>
            Swal.fire({
                icon: 'error',
                title: 'Invalid',
                text: 'Input Fields Cant Be Empty'
            })
        </script>
        <?php
        }
        elseif(isset($_POST['sub'])){
            $login="SELECT * FROM user where username='".$username."' AND password='".$password."'";
            $result = mysqli_query($data, $login);
            $column = mysqli_fetch_array($result);
            $row = mysqli_num_rows($result);
            if($row == 1){
                $_SESSION['username']=$username;
            ?>
            <script>
                Swal.fire({
                    icon: 'success',
                    title: 'Login Succesfully',
                }).then(function() {
                    window.location = "hello.html";
        }           );
            </script>
            <?php
            }
            
            if($column["name"]=="admin"){
                $_SESSION['username']=$username;
            }
            ?>
        <script>
                Swal.fire({
                    icon: 'success',
                    title: 'Admin Succes',
                }).then(function() {
                    window.location = "admin.html";
        }           );
            </script>
            <?php 
        }
}
?>
</form>
</body>
</html>
php sweetalert2
© www.soinside.com 2019 - 2024. All rights reserved.