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

user1017072
  • 81
  • 1
  • 3
  • 11

2 Answers2

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.

NJLaPrell
  • 337
  • 1
  • 6
  • 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. – user1017072 Nov 04 '11 at 14:44
  • 2
    @user1017072, look at http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/ – Mick Sear Nov 04 '11 at 14:51
  • 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(). – NJLaPrell Nov 04 '11 at 15:35
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/

Mick Sear
  • 1,549
  • 15
  • 25
  • ,If I want to avoid JQuery. Can I reverse the approach? Can I call the javascript from the PHP? Thanks – user1017072 Nov 04 '11 at 14:58
  • 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. – Mick Sear Nov 04 '11 at 15:03
  • 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. – Mick Sear Nov 04 '11 at 15:03