Page 1 of 1

php after the page has loaded (post load php)

Posted: Tue Nov 07, 2006 10:48 am
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?

Posted: Tue Nov 07, 2006 11:12 am
by Luke
umm sounds like an AJAX problem... have you heard of AJAX? Look here:
http://blog.coderlab.us/rasmus-30-second-ajax-tutorial/

Posted: Tue Nov 07, 2006 11:56 am
by RICKYJ
I have come across ajax, no idea what it is, Ill have a look at that link, thanks

Posted: Tue Nov 07, 2006 12:04 pm
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.

Posted: Tue Nov 07, 2006 3:33 pm
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

Posted: Tue Nov 07, 2006 3:35 pm
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).

Posted: Tue Nov 07, 2006 3:37 pm
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.

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

Posted: Tue Nov 07, 2006 6:43 pm
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...

Posted: Mon Nov 20, 2006 6:28 am
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

Posted: Mon Nov 20, 2006 6:46 am
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);

Posted: Mon Nov 20, 2006 7:09 am
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>

Posted: Mon Nov 20, 2006 8:26 am
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">

Posted: Mon Nov 20, 2006 2:51 pm
by feyd
Watch the multiple replies. It often looks like you're trying to bump a thread inappropriately.

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

Posted: Mon Nov 20, 2006 3:52 pm
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?