Search code examples
phpsweetalert2

swal.setDefaults is not a function


With this code, I am trying to show all queued alerts.

<?php 
    if(count($_SESSION["alerts"]) >= 1){
        print("    
        <script src='https://cdn.jsdelivr.net/npm/sweetalert2@9'></script>

        <script>
            
            var steps = [");

        for($i = 0; $i < count($_SESSION["alerts"]); ++$i){
            $title = $_SESSION["alerts"][$i][0];
            $message = $_SESSION["alerts"][$i][1];
            $icon = $_SESSION["alerts"][$i][2];
            print("
                    {
                        title:'$title',
                        icon: '$icon',
                        text: '$message',
                    },
            ");
        }

        print("
            ];
            
            swal.setDefaults({
                confirmButtonText: 'Ok',
                showCancelButton: false,
                animation: true
            });


            swal.queue(steps);

            </script>
        ");
    }
    $_SESSION["alerts"] = null;
    $_SESSION["alerts"] = array();        
?>

But if I call this with an alert in the queue, then I see an error message in the chrome console: swal.setDefaults is not a function

How can I make this code work as intended?


Solution

  • swal.setDefaults({ }) was deprecated in v8 and removed in v9. Use swal.mixins({...options});

    see the docs for more. https://sweetalert2.github.io/#mixin;

    here is the git hub reference also https://github.com/sweetalert2/sweetalert2/issues/530