Limon
Limon

Reputation: 1793

redirect after alert message not working

this is a snippet of a js file>

$('#btnYes').on('click', (function() {
var id = $('#myModal').data('id');
var usertype = $('#myModal').data('usert');

$.ajax({

  url: '{site_url()}admin/deleteUserFromList',
  type: 'POST',
  data: {id: id, userT: usertype},
  success: function(html){

    $('[data-id='+id+']').parents('tr').remove();
    $('#myModal').modal('hide');
    alert('usuario borrado');
    window.location.reload();
  }
});
 return false;

}));

as you can see there is an alert message after deleting a user from a list.

I want to refresh the page after ok on alert message is pressed so i added the line>

window.location.reload();

but it's not working, why is this? how can i fix it?

I've been trying to use alternative to this like

location.href = '....';

window.location = '/some/url';

but nothing seems to work!

this is in my admin.php, the code for deleting user from the database:

public function deleteUserFromList(){

    if ((isset($_POST['id']) && (isset($_POST['userT'])))){
        $rowId = $_POST['id'];
        $userType = $_POST['userT'];

        $result = array();

        if($userType == 'front'){
            $front = UserManager::getInstance()->getUser($rowId);
            UserManager::getInstance()->deleteItem($front);

        }else{
            $back = UserBackManager::getInstance()->getUser($rowId);
            UserBackManager::getInstance()->deleteItem($back);
        }
        $result["message"] = "Usuario eliminado";

        echo json_encode($result);
    }
}

Upvotes: 3

Views: 1828

Answers (2)

Khamidulla
Khamidulla

Reputation: 2975

In order to simulate redirect in your browser try to:

Javascript way:

window.location.replace("http://stackoverflow.com");

jQuery way:

var url = "http://stackoverflow.com";    
$(location).attr('href',url);

Try this and let me know it it works for you or not.

EDIT: Inside ajax success. Try to close modal window and try to replace method.

EDIT 2: Put this part of code inside of your document ready block and check is it fired or not if it is fired it means your form is reloading correctly.

$( window ).unload(function() {
  alert( "Bye now!" );
});

Upvotes: 1

Carmax
Carmax

Reputation: 2927

Elaborating on @charlietfl's comment, could you try something like this?

Return the count from the ajax script and insert it into your page:

$('#btnYes').on('click', (function() {
  var id = $('#myModal').data('id');
  var usertype = $('#myModal').data('usert');

  $.ajax({

    url: '{site_url()}admin/deleteUserFromList',   // this returns the user count as data
    type: 'POST',
    data: {id: id, userT: usertype},
    success: function(data){

      $('[data-id='+id+']').parents('tr').remove();
      $('#countDisplayElement').text(data);  // insert new count into current page
      $('#myModal').modal('hide');
      alert('usuario borrado');
      window.location.reload();
    }
  });
 return false;

}));

That would eliminate the need to refresh the page entirely and be a bit more friendly to use.

Upvotes: 0

Related Questions