php after the page has loaded (post load php)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

php after the page has loaded (post load php)

Post by RICKYJ »

Is there a way of getting a page to do the php comand after the rest of the page has loaded and been diplayed. I have found refrenced to iframe and DHTML, but im not sure how to do this yet.

Basicaly I have a stock ticker that downloads RSS feed to a local folder, this takes quite a while, how ever this would be acceptable if the php downloader was to run while the rest of the page was being displayed, anyone know how to do this, or what i should be looking at?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

umm sounds like an AJAX problem... have you heard of AJAX? Look here:
http://blog.coderlab.us/rasmus-30-second-ajax-tutorial/
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

I have come across ajax, no idea what it is, Ill have a look at that link, thanks
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

It's a pretty simple concept... basically javascript sends a request to a page, grabs it's response, and parses it... not a whole lot to it.
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

ok, Ive been reading a bit on ajax. It seems to run javascript like the onload function (once the page is loaded), but can you use javascript to call up a php function.

php is calculated before it reaches the browser, so im wondering if this is possible at all
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ajax makes a request to the server which would handle the php and send a response back to the client. It's just like opening a new page (or refreshing the one you're on).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yes, and it doesn't have to be an onload at all... any type of event can trigger an ajax request. read that tutorial.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: php after the page has loaded (post load php)

Post by timvw »

I think you're better of with a script that runs on the background to fetch the RSS feeds. And then let your webpages use those cached versions... Downloading them for every client will get your host banned from most feeds anyway...
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

It doesnt download for each client, the php checks the modifaction of the local files (TempRssFeed) and only downloads if more than 7hrs old, or different day.

Still havent got it to work yet, but figuring it out
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

almost there I think:
Saved in the local file as JSsource.js:

Code: Select all

function createRequestObject() {
     var ro;
     var browser = navigator.appName;
     if(browser == "Microsoft Internet Explorer"){
          ro = new ActiveXObject("Microsoft.XMLHTTP");
     }else{ro = new XMLHttpRequest();}
     return ro;
}

var http = createRequestObject();

function sndReq(action) {
     http.open('get', 'fetchRSS2.php?action='+action);
     http.onreadystatechange = handleResponse;
     http.send(null);
}

function handleResponse() {
     if(http.readyState == 4){
          /* nothing here, since the page doesnt have to be updated */
     }
}
-------------------------------
on my page that loads the data use:

<BODY onLoad="javascript:sndReq()" >
<script type="text/javascript" src="JSsource.js"></script>
-----------------------------
the php code (fetchRSS2.php) contains:

Code: Select all

function file_put_contents_php4 ($location, $whattowrite) {
    if (file_exists($location)) {
        unlink($location);
    }
    $fileHandler = fopen ($location, "w");
    fwrite ($fileHandler, $whattowrite);
    fclose ($fileHandler);
}

 echo file_put_contents_php4("aFile.txt",$str);
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

works, but is it updating once the page is loaded (its hard to tell as my broadband is too fast, obvioulsy not everyones will be)

this litle bit here:

Code: Select all

<BODY onLoad="javascript:sndReq()" >
<script type="text/javascript" src="JSsource.js"></script>


Does this run as on load only, or is the sript run as soon as it hits:

Code: Select all

<script type="text/javascript" src="JSsource.js"></script>
Last edited by RICKYJ on Mon Nov 20, 2006 8:27 am, edited 1 time in total.
RICKYJ
Forum Newbie
Posts: 15
Joined: Sun Nov 05, 2006 9:10 am

Post by RICKYJ »

anyone know if this is the method that i should be using in javascript?

Code: Select all

<BODY onLoad="javascript:sndReq()" >
<script type="text/javascript" src="JSsource.js"></script>
or should it be something like

Code: Select all

<BODY onLoad="javascript:sndReq()" type="text/javascript" src="JSsource.js">
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Watch the multiple replies. It often looks like you're trying to bump a thread inappropriately.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: php after the page has loaded (post load php)

Post by RobertGonzalez »

RICKYJ wrote:Basicaly I have a stock ticker that downloads RSS feed to a local folder, this takes quite a while, how ever this would be acceptable if the php downloader was to run while the rest of the page was being displayed, anyone know how to do this, or what i should be looking at?
Why is it taking quite a while? Wouldn't it make sense to optimize the download as opposed to writing a new codebase to allow for slow loads in the background?
Post Reply