Skip to content
Advertisement

Page Reloading after data update using AJAX & also ALERT not showing after data update

I am trying to update some data in using PHP & AJAX but my alert is not showing. In function(response) i am getting “success” message from PHP file. Please advise

<link rel="stylesheet" 
<body>
  <div style="margin: auto;width: 60%;">
    <div class="alert alert-success alert-dismissible" id="success" style="display:none;">
      <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
</div>   

———- MY AJAX SCRIPT ———-

<script>
$(document).ready(function(){
    $(".upr").click(function(){
        var $form = $(this).closest('form');
        var request=$form.find('[name="request"]').data("request");
        var userid=$form.find('[name="userid"]').data("userid");
        var pr=$form.find('[id="pr"]').val()
        
        //alert(+pr+"/"+userid+"/"+request);
            $.ajax({
            url:'update.php',
            method:'POST',
            data:{
                request:request,
                userid:userid,
                pr:pr
                
                },
                success:function(response){
                 //alert(response);
                 $("#success").show();
                $('#success').html(response);
                
                }
            });
    });
});
</script>

Advertisement

Answer

You need to check Network tab in your browser and ensure you have 200+ HTTP code when AJAX produced, otherwise you won’t reach success callback. Also, you can add error callback and do some console.log. This might give you some clue what is happening:

$.ajax({
    url:'update.php',
    method:'POST',
    data:{
        request:request,
        userid:userid,
        pr:pr

    },
    success:function(response){
        //alert(response);
        $("#success").show();
        $('#success').html(response);

    },
    error:function(jqXHR, textStatus, errorThrown) {
        console.log('Error happened', jqXHR, textStatus, errorThrown);
    }

});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement