3

I am trying to display an alert message after the record is inserted in the datbase and normal js alert is working fine the code for this is

echo '<script>';
echo 'alert(" Your request has been successfully filled and your Letter No is\n'.$Dep.'/'.$fy.'/ "+"'.$id.'.\nKindly submit the original copy of this letter to CRDS.")';
echo '</script>';
echo "<script>window.location.href='dashboard.php'</script>";
exit;

But when I am using sweet alert function instead of normal js function it does not display any message.What is wrong in my code> I have included sweet alert library in my code.My code is

echo '<script>';
    echo 'swal(" Your request has been successfully filled and your Letter No is\n'.$Dep.'/'.$fy.'/ "+"'.$id.'.\nKindly submit the original copy of this letter to CRDS.")';
    echo '</script>';
    echo "<script>window.location.href='dashboard.php'</script>";
    exit;
gorakh
  • 59
  • 3
  • I'm not familiar with SweetAlert, but you might find this informative: [Why does Alert function execute faster than any other functions in javascript?](https://stackoverflow.com/questions/51337068/why-does-alert-function-execute-faster-than-any-other-functions-in-javascript) Also see [Stop the control while SweetAlert is on screen](https://stackoverflow.com/questions/40362725/stop-the-control-while-sweetalert-is-on-screen). – showdev Apr 03 '23 at 02:43
  • @showdev Thnx but not helpful – gorakh Apr 03 '23 at 02:51
  • I'm thinking the page might be redirecting before the SweetAlert fires. Does that seem to be the case? That link suggests using a promise returned by SweetAlert, rather than redirecting the page immediately. – showdev Apr 03 '23 at 02:52

3 Answers3

1

In your headings,

Add,

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

Then,

echo '
    <script type="text/javascript">
        $(document).ready(function(){
            swal({
                title: "Successfully save!",
                html: "Your request has been successfully filled...",
                timer: 2000,
                timerProgressBar: true,
            }).then(function() {
                window.location = "( Your link here. )";
            });
        });
    </script>
';

Newbee
  • 702
  • 1
  • 13
  • It is not working .Can you look at this code once plz – gorakh Apr 04 '23 at 17:33
  • I tried this code it is not working.it is showing error "Uncaught ReferenceError: $ is not defined" – gorakh Apr 04 '23 at 17:47
  • I edited my code. You can copy paste my code [here](https://www.w3schools.com/php/phptryit.asp?filename=tryphp_intro) to see the result. – Newbee Apr 05 '23 at 00:35
0

most likely the swal function is wrong, look at the swal documentation and here is an example of how to use it, maybe you are missing the fire.

Swal.fire(
  'Good job!',
  'You clicked the button!',
  'success'
)

that on that side of the alert, however if we look at the topic more in depth, in the last line you are changing the url with the window.location.href, then it would be good to comment or implement it differently because there you are redirecting it to another page and the alert will not be displayed.

  • I have gone through that but I could not find any solution for my case.When i am using php variable in the messsage it is creating problem – gorakh Apr 03 '23 at 02:53
0

using SweetAlert you can just simply use the:

echo 'swal("Some'.$variable.'text!", "You clicked the button!", "success")';

but in SweetAlert2 you can just simply use:

echo ;Swal.fire(
  'Some'.$variable.'text!',
  'You clicked the button!',
  'success'
)';


You can try this one
    `echo "<script>
                Swal.fire({
                        title: 'Successfully save!',
                        html: `Your request has been successfully filled and your Letter No is\n 
                         ${$Dep}/${$fy}/${$id}\nKindly submit the original copy of this letter to 
                          CRDS..`,
                         timer: 2000,
                         timerProgressBar: true,
                        }).then((result) => {
                      window.location.href='dashboard.php'
               })
             </script>"
             `

XRT-NoOne
  • 46
  • 8