Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am using sweetalert2 in my codeIgniter4 Code but its not firing the alert, kindly help me, thanks in advance.

css and js files included correctly in relative places i.e css in head and js at end of body tag.

Controller Code:
on successful login

PHP
session()->setFlashdata('success',"Welcome to Dashboard!");


LayOut file:

HTML
<link rel="stylesheet" href="<?=site_url('public/template/') ?>plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">

<script src="<?=site_url('public/template/') ?>plugins/sweetalert2/sweetalert2.min.js"></script>


What I have tried:

JavaScript
<script>
 $(function() {
     // Sweet Alert
     swal = Swal.mixin({
      swal: true,
      position: 'top-end',
      showConfirmButton: false,
      timer: 3000
    });

    //Sweet Alert
    <?php if(session()->has('success')){
      ?> 
      swal.fire({
        icon: 'success',
        title: 'Good',
        text: '<?php echo session()->getFlashdata('success'); ?>'
      });
    <?php } ?>
	
    <?php if(session()->has('error')){ ?> 
      swal.fire({
        icon: 'error',
        title: 'Oops',
        text: '<?=session()->getFlashdata('error'); ?>'
      })
    <?php } ?>
    
  });
</script>
Posted
Updated 9-May-24 4:56am
v2
Comments
Chris Copeland 9-May-24 9:58am    
Have you tried checking the console in the browser to see if there are any errors? I can't see anything immediately wrong with the code, however the browser console will give you more information.
nyt1972 9-May-24 10:55am    
Thanks alot, problem solved there was an error in script above these lines.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900