I am trying to run a php file when a javascript function is called and then reload the page, it reloads the page but the php file doesn't get executed.
function changeR()
{
$.ajax({ url: 'win.php?id=1' });
window.location.reload()
}
I am trying to run a php file when a javascript function is called and then reload the page, it reloads the page but the php file doesn't get executed.
function changeR()
{
$.ajax({ url: 'win.php?id=1' });
window.location.reload()
}
Do the reload once ajax call is completed. You may use the success event.
function changeR()
{
$.ajax({ url: 'win.php?id=1' ,
success: function(data) {
window.location.reload()
}
});
}
keep in mind that success event will be executed if the ajax call is success, if you want to execute some code no matter it is success or error, you may consider, [complete][1] or done
Alternatively you can try window.location.href=window.location.href instead of window.location.reload()
Usually folks use ajax to do partial page updates without a page reload. Are you sure you want to reload the entire page after your ajax call ?
EDIT : As per the comment
If you want to pass your php variable value via this ajax call , you can do like this
Set the value to some html element ( ex : a hidden element with id txtID )
//Reading the value of an html element with id `txtId`
var val=$("#txtID").val();
$.ajax({ url: 'win.php' ,
data: { id: val },
success: function(data) {
window.location.reload()
}
});
Here you are send the value of element in a parameter named id. You can use firebug or fiddler to see what value is going.
More options are here : http://api.jquery.com/jQuery.ajax/
First of all make sure there are no errors in your win.php script that could prevent it from successfully executing.
Second, only perform the reload after the ajax call is completed. An easier way to accomplish this is by setting the asyc setting to false. This way the call is done synchronously, meaning it will wait for its execution to complete before continuing.
function changeR()
{
$.ajax({ url: 'win.php?id=1', async: false });
window.location.reload()
}
try this into your 'changeR' function:
$.ajax({
type: "GET",
url: "win.php",
data: "id=1",
success: function(msg){
window.location.reload();
}
});
success will be deprecated in 1.8. Instead, you should use done.