Neel Thakkar Neel Thakkar - 4 months ago 25
Ajax Question

Alert box does not work

I am trying to display an alert box and click on Ok to refresh the page. I have tried using

window.location.reload(true)


I want to show a message before page reload. The problem is that the page reloads automatic but the alert box not showing.

HTML Content

<form action="<?php echo $action_link; ?>" method="post" name="form_Change_Password" id="form_Change_Password" class="form-horizontal" novalidate="novalidate">

<input type="hidden" name="id" value="<?php echo $admin_id; ?>">
<div class="form-body">
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button> You have some form errors. Please check below.
</div>
<div class="alert alert-success display-hide">
<button class="close" data-close="alert"></button> Your form validation is successful!
</div>
<div class="form-group margin-top-20">
<label class="control-label col-md-3">Old Password
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="password" class="form-control" name="old_password" id="old_password" value="">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">New Password
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="password" class="form-control" name="new_password" id="new_password" value="">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Confirm Password
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="password" class="form-control" name="confirm_password" id="confirm_password" value="">
</div>
</div>
</div>
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-offset-3 col-md-9">
<button type="button" class="btn green" name="btnChangePassword" onclick="newpassword(<?php echo $admin_id; ?>)">Update</button>
<a href="<?php echo $admin_url; ?>index.php?file=a-admin_profile" class="btn default">Cancel</a>
</div>
</div>
</div>
</form>


Ajax Code:

$.ajax({
url: "<?php echo $action_link; ?>",
type: "POST",
data: {
'id': <?php echo $admin_id; ?>,
'old_pass': old_pass,
'new_pass': new_pass,
'conf_pass': conf_pass,
change_password : 'change_password'
},
success: function (result){
if(result == 1){
bootbox.alert('Password changed successfully.');
window.location.reload(true);
}else if(result == 2){
bootbox.alert('New password and confirm password does not match.');
return false;
}
else if(result == 0){
bootbox.alert('Old password does not match.');
}
},
error: function (result){
}
});


Action

if(isset($_REQUEST['change_password']) && $_REQUEST['change_password'] == 'change_password'){

ob_get_clean();
$id = $_REQUEST['id'];

$password = mysqli_real_escape_string($obj->CONN,md5($_REQUEST['old_pass']));

$dbpassword = $admin->select_password($id);

$newpass = $_REQUEST['new_pass'];
$confpass = $_REQUEST['conf_pass'];

if($dbpassword != $password){
echo 0; exit;
}elseif($newpass != $confpass){
echo 2; exit;
}
else{
$admin->update_password($id, md5($newpass), $current_date);
echo 1; exit;
}
}

Answer

You have to use bootbox callback :

bootbox.alert("Password changed successfully.", function() {
    // any code you want to happen after the alert is dismissed
   window.location.reload(true);
});

So it will be :

...
success: function (result){
    if(result == 1){
        bootbox.alert("Password changed successfully.", function() {
            window.location.reload(true);
        });
    }else if(result == 2){
        bootbox.alert("New password and confirm password does not match.", function() {
            return false;
        });
    }else if(result == 0){
         bootbox.alert('Old password does not match.');
    }
},
...

Hope this helps.

Comments