indymx
indymx

Reputation: 343

jQuery sweetalert2 function does not

This worked just fine with the original sweetalert, but with sweetalert2 it never runs the function to reload my ajax data.

Can anyone see an error?

         .on('click', '#saveRider', function (e) {
            e.preventDefault();
            $.ajax({
                url: 'ajax/saveRiderClass.php',
                dataType: 'JSON',
                type: 'POST',
                data: $('#addRidersForm').serialize(),
                success: function (json) {
                    if (json && json.status && json.already_entered === false) {
                        swal({
                                title: "Rider Saved and Classes Added!",
                                type: "success"
                            },
                            function (isConfirm) {
                                if (isConfirm === true) {
                                    $("#enteredRiders").load("ajax/getEnteredRiders.php");
                                    $("#uniqueRiders").load("ajax/getUniqueRiders.php");
                                    $("#totalRiders").load("ajax/getTotalEntries.php");
                                }
                            });
                    }
                    else if (json && json.status && json.already_entered === true) {
                        swal({
                            title: "Rider already entered in that class",
                            type: "warning"
                        });
                    }
                }
            });
        })

Upvotes: 1

Views: 1985

Answers (1)

ChandlerBing42
ChandlerBing42

Reputation: 164

I suggest you take a look at the migration guide from sweetalert to sweetalert2. Sweetalert2 does not use callbacks anymore but promises instead.

You can find all the migration details here

EDIT: Here's how you should modify your code. I'm taking the first sweetalert call you do.

This call:

swal({
        title: "Rider Saved and Classes Added!",
        type: "success"
      },
      function (isConfirm) {
          if (isConfirm === true) {
              $("#enteredRiders").load("ajax/getEnteredRiders.php");
              $("#uniqueRiders").load("ajax/getUniqueRiders.php");
              $("#totalRiders").load("ajax/getTotalEntries.php");
          }
      });

should become something like that:

swal({
        title: "Rider Saved and Classes Added!",
        type: "success"
      }.then(function () {
          $("#enteredRiders").load("ajax/getEnteredRiders.php");
          $("#uniqueRiders").load("ajax/getUniqueRiders.php");
          $("#totalRiders").load("ajax/getTotalEntries.php");
      });

Notice the .then before the function()

Using .then you are using promises just as sweetaler2 expects instead of callbacks.

Upvotes: 3

Related Questions