|
-
Dec 20th, 2010, 04:32 PM
#1
Thread Starter
Hyperactive Member
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
-
Dec 20th, 2010, 07:24 PM
#2
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.
-
Dec 25th, 2010, 10:00 AM
#3
Thread Starter
Hyperactive Member
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
-
Dec 26th, 2010, 01:00 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|