Skip to content
Advertisement

Sweetalert2 redirection

I’m struggling with this for a few hours already and I think that the solution is very simple. First a bit of background.

So, I have a php page where I can enter a number in a texfield. If the number entered is higher than the credit the user has, then I have to show an error message. When the user clicks on the button to send the form, I go to makeTransfer.php and I do several checks in here. If, as I said, the user has not credit enough I want to show him an error message using SweetAlert2 and then redirect him to the previous page (let’s call it cashier). At the moment, I can show the alert but I’m unable to redirect him :(.

Here is the code:

echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("WOW!","Message!","error");';
echo '});</script>';

Any help, please? I just want to show the alert, and as soon as the user clicks on Accept button, redirects him to cashier.php

Thanks in advance!

Advertisement

Answer

Ok, solved the problem. Here is the code:

<script type="text/javascript">
    $(document).ready(function() {
swal({ 
  title: "Error",
   text: "wrong user or password",
    type: "error" 
  }).then(function() {
    // Redirect the user
    window.location.href = "cashier.php";
    })});
</script>

Worked my a** off, but it paid off :). Thanks everyone who tried to help me.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement