Sweetalert JS redirect with window location after update in php

6.8k Views Asked by At

I want to make my prompt message a little bit more fancy or good. I am using PHP as my back-end in my project but I am only using a normal prompt alert after successful operations. I will not include all the code here only in the part of sweetalert js.

After successful update operation. I want to display an alert message using sweetalert swal after I updated the user profile and when I click the ok button in the swal it will refresh the page to see the changes.

    echo "<script>swal('Successfully updated the profile!', 'Click ok to refresh the page.', 'success');
window.location='preschooler_profile.php?p_id=$p_id'</script>";

After the update operation everything works fine in the server. But the prompt alert swal by sweetalert won't popup. But when I tried to test the sweetalert in the top of my html file and its working. So no problem with the sweetalert maybe because of the window.location?

Looking for help. Thanks in advance.

3

There are 3 best solutions below

1
On BEST ANSWER

Why don't you try echoing this instead?

   swal({
      title: "Successfully updated the profile!",
      text: "Click ok to refresh the page.",
      type: "success"
    },
    function(){
      window.location='preschooler_profile.php?p_id=$p_id'
    });
0
On

The third argument of swal() is the type! Try something like this:

swal({
  title: "Successfully updated the profile!",
  text: "Click OK to refresh the page.",
  type: "success",
  confirmButtonText: "OK",
  closeOnConfirm: false
},location.reload.bind(location));
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>

0
On

Try with echo in php

  swal({
      title: "Successfully updated the profile!",
      text: "Click ok to refresh the page.",
      type: "success"
    },
    function(){
      window.location='preschooler_profile.php?p_id=<?php echo $p_id ?>';
    });