Results 1 to 4 of 4

Thread: call php function after page loaded(printed)?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    call php function after page loaded(printed)?

    Hi! I have a page that contains a regular table, the data of the first column of the table is known and written in the source code of the page. The 2nd column needs data to be loaded from net (from another site)... Reading these data from a remote server may takes long time... because of that I want that the page is printed then start retreive remote data and add them to the page after finish receiving.

    I tried this:
    Code:
    <script>
    window.onload = function (){
    	<?php
    		function doload(){
    			$remotedata = readfromsite("http://mysite.com/myfile.txt");
    			echo "document.getElementById('mydiv').innerHTML = '".$remotedata."';";	
    		}
    		doload(); 
    	?>
    }
    </script>
    The code is work but the same problem, the page didn't load before the whole remote data retreived! how to make the page loads (printed) and then call that doload() php function?

    Thank's

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: call php function after page loaded(printed)?

    The way you're trying to do this will not work because at the time a page is rendered by the client, all PHP has already been processed and is effectively gone - only the resulting output of your PHP remains.

    It's still possible to do what you want though. You'll need to use a process known as AJAX, which involves having Javascript call upon a script (PHP) and do something with its output. Please take a look at this tutorial to get started.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Re: call php function after page loaded(printed)?

    Thank's alot. I did it using AJAX. But the problem is that I print the content in the page in order to allow ajax reading it by using xmlhttp.responseText

    is there any more secure way? because the data retreived and printed is not supposed to be visible for the user. However, I protect the page using password sent in the requested address as ($_GET)... so once the user know that password, he will be able to access to the secured data.

    Thank's

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: call php function after page loaded(printed)?

    so you just have to secure the page that retrieves the information as well. then, you just change the URL you're sending a request to. if you put the password in the query string, and your data-retrieving script was data.php, then you might do something like:
    PHP Code:
    ajax.open("GET", "data.php?password=<?php echo $_GET['password']; ?>", true);
    this way, you can't view data.php without having a password either.
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width