在 PHP 重定向后使用 sweetalert

Using sweetalert after PHP redirect

我想在错误登录后使用 sweetalert。不工作..我使用正确的 CDN 吗?

login.php

if(password_verify($passwordInput, $dbPasswd)) {
    header('Location: ../store.php');
} else {
    header('Location: ../index.php?login=false'); //redirect to index.php and show sweetalert
}

index.php:

<head>
    <script src="jquery-3.3.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
</head>
if(isset($_GET['login']) == 'false') {
    echo '<script type="text/javascript">$(document).ready(function() {
        swal({
           title: "Your title",
           text: "whatever",
           type: "error"
        });
     });</script>';
}

我通常在发出 ajax 请求时使用带有这种语法的 sweet alert。这是示例:

            $.ajax({
                type: "PATCH",
                url: "myurl.php",
                contentType: "application/json",
                data: data,
                success: function (data) {
                    swal({
                        title: 'Success',
                        text: 'whatever',
                        type: 'success'
                    });
                },
                error: function (xhr, error) {
                    swal({
                        title: xhr.responseJSON.title,
                        text: xhr.responseJSON.detail,
                        type: 'error'
                    });
                }
            });

所以在你的情况下你应该有这样的东西:

<head>
    <script src="jquery-3.3.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
</head>
<?php
if(isset($_GET['login']) == 'false') {
?>
  <script type="text/javascript">
    $(document).ready(function() {
       swal({
          title: 'Your title',
          text: 'whatever',
          type: 'error'
       });
    });
  </script>
<?php
}
?>