Basically problem is in your $.ajax
request.
As you know $.ajax
by default perform an asynchronous HTTP (Ajax) request.
Easiest solution for you is to make request synchronous.
To make request synchronous you should set option async: false
- in this case code will be executed linearly and you will get return response;
only when ajax request is completed.
So basically change part of ajax call to:
$.ajax({
type: "POST",
url: "User.php",
async: false,
data: {
'slug': value,
'id': <?php echo $id; ?>
},
dataType:"html",
success: function(msg){
response = msg === 'true';
}
});
May be useful: