如何在重定向到另一个页面后显示警报

时间:2018-02-09 09:20:30

标签: javascript php

登录表单(index.php)已创建。提交表单时,用户名和密码将传递到另一个页面(login-controller.php)。验证后,如果用户名或密码不正确,用户在login-controller.php中显示警告框后被重定向到登录表单(index.php)。但是我想显示一个警告框,如"无效的登录凭据&#34 ;重定向到登录表单后(index.php)。我怎么能解决这个问题。 login-controller.php的代码如下所示。



<?php

session_start();
if (isset($_SESSION['username'])) {
    header('Location: ../service/index.php');
}

include '../include/conn.php'; //database connection

if (isset($_POST["submit"])) {
    if (empty($_POST["username"] && $_POST["password"])) {
        header('Location: index.php');
    } else {
        $username = mysqli_real_escape_string($conn, $_POST["username"]);
        $password = mysqli_real_escape_string($conn, $_POST["password"]);
        $password = md5($password);
        $sqli = "SELECT * FROM user WHERE name = '$username' AND password = '$password'";
        $result = $conn->query($sqli);
        $count = mysqli_num_rows($result);
        if ($count == 1) {
            $_SESSION['username'] = $username;
            header('Location: ../service/index.php');
        } else {
            include '../include/bootstrap.php'; //bootstrap js and css
            echo '<div class="alert alert-danger"><strong>Oops!</strong> Invalid Login Credentials.</div>';
            echo "<script>setTimeout(\"location.href = 'index.php';\",3000);</script>";
        }
    }
}
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

使用会话变量可以显示警报:

$_SESSION['alerts'][] = "Test Alert";
$_SESSION['alerts'][] = "Another Test Alert";

然后在您的配置中,或者在您拥有的每个页面上执行的任何其他.php文件(如果您有模板系统,请在模板中执行此操作):

if(isset($_SESSION['alerts'])){
    foreach($_SESSION['alerts'] as $alertmessage){
        echo '<div class="alert">' . $alertmessage . '</div>';
    }
}
相关问题