0

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()
}
2
  • 1
    What makes you say that the PHP script isn't executed? Are you sure it isn't an error in the PHP script preventing it from running? Commented Apr 17, 2012 at 23:53
  • Ive successfully run the php file separately and it properly updates a mySQL server, when I do this, there is no change to the mySQL server Commented Apr 17, 2012 at 23:59

4 Answers 4

4

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/

Sign up to request clarification or add additional context in comments.

7 Comments

That didn't work, is there anything you need to load before .ajax?
Is there any other script errors ? in your page ? use firebug console to see what is the error
@AaronBoswell: I had a script error (an extra closing bracket). Fixed that.try now
@AaronBoswell : Make sure you have jQuery loaded properly
Thanks, is there anyway way to pass the id=1 through as another parameter of .ajax? If so is there anyway to make the '1' be a $id, a php variable I have stored earlier in the page?
|
1

The AJAX calls are asynchronous, meaning you perform window.location.reload() before the AJAX request has finished. Instead, wait until the AJAX request is done, then reload.

function changeR() 
{
  $.ajax({
    url: 'win.php?id=1'
  }).done(function() {
    window.location.reload()
  });
}

Comments

0

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()
}

Comments

0

try this into your 'changeR' function:

$.ajax({
type: "GET",
url: "win.php",
data: "id=1",
success: function(msg){
  window.location.reload();
}
});

1 Comment

success will be deprecated in 1.8. Instead, you should use done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.