DarkFelcore
DarkFelcore

Reputation: 49

Using sweetalert after PHP redirect

I want to use a sweetalert after a bad login. Not working.. Do I use correct 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>';
}

Upvotes: 1

Views: 916

Answers (1)

Angel Deykov
Angel Deykov

Reputation: 1227

I use sweet alert with this syntax, usually when making ajax requests. Here is example:

            $.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'
                    });
                }
            });

So in your case you should have something like:

<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
}
?>

Upvotes: 1

Related Questions