0

I have this simple function in javascript to refresh a page after a set time.

function AutoRefresh( t ) {
    setTimeout("location.reload(true);", t);
}

Now after every refresh, I want it to call a PHP function, for example:

function writeName()
{
echo "Refresh me";
}

Is it possible to call the PHP function in JavaScript after every refresh? Thanks

2

2 Answers 2

1

I'm afraid the answer is no. By the time JavaScript has been run on your page, the server side (PHP) has already finished processing. Your best bet is to accomplish what you need before the page load, or accomplish it with JavaScript alone.

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

3 Comments

What would be the best approach for scenario like this: I have a PHP script which connects to a db to get data, I want it to get the data every few seconds and display on screen. I don't want the user to refresh the screen manually, so I would prefer to have a javascript to refresh the page automatically to display the change.
As stated above, your best bet is to use an AJAX call at regular intervals. jQuery is easy to get up and running quickly. If you are looking to just update content, check out the $.load() function to load the content directly into a section of the page. For more complex things, make a PHP page that outputs JSON and use $.getJSON().
0

If you are refreshing the page, you're effectively reloading it from the server, so any 'onload' events will fire again. The page will render again from scratch. You can call a PHP script using AJAX in some 'onload' Javascript listener if you like, though. e.g. with JQuery:

 <html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">

    $(document).ready( function () {

        setTimeout("location.reload(true);", 2000);

        $.get('home.html', function(ret){
            $('body').html(ret);
        });

    });

    </script>
    </head>
    <body>
        <h1>Test</h1>

    </body>
    </html>

Beware of calling setTimeout() recursively though as it can make a page unresponsive over time. You may find this useful:

http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/

3 Comments

,If I want to avoid JQuery. Can I reverse the approach? Can I call the javascript from the PHP? Thanks
Not really. Read the article linked by NullUserException above. Think about what code executes on the server (PHP), and which code is simply hosted on the server but is actually executed on the client (JS). They are very different, and joining the two is HTTP, which is a stateless protocol.
Of course, you don't need to use JQuery. You can write your own JS function if you like, or use a different framework to save you time.

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.