Alert bootstrap in ajax call

Asked

Viewed 1,304 times

0

I have an ajax that deletes an image, after the deletion, I would like the page to give a Reload and show a message of "Deleted Successfully!" with Alert boostratp. I’ve searched the forums and didn’t find anything that would help me.

jQuery(document).ready(function() { 
    jQuery('#teste').submit(function(){
        var dados = jQuery( this ).serialize();

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "crud/excluirImagem.php",
            data: dados,
                success: function(data) {
                    location.reload();
            }
        });

    });

});

Bootstrap alert

A solution

 jQuery(document).ready(function() { 
    jQuery('#teste').submit(function(){
    var dados = jQuery( this ).serialize();

 $.ajax({
    type: 'POST',
    dataType: 'json',
        url: "crud/excluirImagem.php",
        data: dados,
        success: function(data) {
        $('.alert').fadeIn('2000');
            setTimeout(function(){ reloadPagina() }, 3000);

            }
        });
        return false;
    });
});

function reloadPagina() {
    location.reload();
}

2 answers

1


You can try to do the following:

  • Create image delete function!
  • In AJAX, you don’t need to re-load (try to do it without Reload)!
  • Within the .success or .done try to make Alert appear using the functions fadeIn('slow'); and fadeOut('slow');
  • After giving fadeOut, just give the Reload on the page, simple! Any questions just ask!

Example

$.ajax ({
        type: 'POST',
        dataType: 'json',
        url: "crud/excluirImagem.php",
        data: dados,
            success: function(data) {
                $('#alert').fadeIn('2000');
                //Intervalo
                $('#alert').fadeOut('5000');
        }
    });

0

No need to re-load.

html

<div id="alert" class="alert alert-success" role="alert"></div>

Javascript

$(function () {
 $("#alert").css('display', 'none');
 $('#teste').submit(function(){
   var dados = jQuery( this ).serialize();  
   $.ajax({
     type: 'POST',
     dataType: 'json',
     url: "crud/excluirImagem.php",
     data: dados,
     success: function(data) {
       $("#alert").css('display', 'block');
       $("#alert").html('sua_msg_aqui!');
     }
   });
 });
});

Tb has Alert-dismissible. Take a look at bootstrap Ocs.

  • I am obliged to give Reload, to "disappear" the images that have just been deleted.

  • 1

    Just create the Alert on the same page, and then put a 10-second interval or less, then just give the Reload!

  • 1

    @Alisson you can also delete the images with . remove() or . Hide() in ajax’s Success...

  • @Jader-a-Wagner hinted

  • A solution was found in the question I edited, what do you think?

  • I think you should mark the question as answered...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.