Forum Moderators: coopster

Message Too Old, No Replies

Can PHP update a file without reloading/refreshing it?

         

JAB Creations

12:03 am on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can PHP update a file without reloading/refreshing it?

Tapolyai

12:13 am on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you updating a file from a PHP file? Then yes.
Are you updating a PHP page? Then possibly.

Can you give a bit more description?

ironik

12:30 am on Apr 5, 2005 (gmt 0)

10+ Year Member



PHP is a stateless language, it'll only run when told to (someone/something requests a page, so the server's php module parses the page and outputs it to the browser). If you are looking at something where you don't have to reload an entire page to make use of php's coding abilities (make it look like it's acting in real time) you could look into XMLhttprequest (javascript), XUL (mozilla only) or some creative work with frames... I hate frames though.

Thos other options are pretty cool, but they only appear to make the page look like it's working in real-time, the way php behaves never actually changes.

JAB Creations

3:10 am on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Everyone has to badmouth frames but no one has yet to suggest something worthy to replace them live...

Anyway... my site IS frames so I'd like to see if it's possible to have say my news php file have it's text updated with the name of the artist/song playing ... the song being played of course on another php file in another frame.

So when a new song loads (each song has it's own page) the page sends the other php page the name of the artist and song playing. Can that be done without refreshing or reloading the page that the person may be reading at the moment?

ironik

3:29 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Not sure if I understand correctly, but you have a song that loads inside a frame and when that frame is reloaded you want it to change information located in another frame? Only way I can see around that is to trigger some javascript when the page loads so it can talk to the other frame... maybe I'm way off here. Is that what you wanted to do?

I think everyone bad mouths frames for a reason, they're bad for users, search engines and programmers. Sure, they have their uses (I still use them for documentation where I have a javascript tree that cannot maintain state between page loads), but there are worthy replacements already.

You can do the song reload thing with XMLHttpRequest, just tell it to load a php file. Only thing is you are limited for browser compatibility.

When we see things like XUL and Apache Ant mature and enter mainstream programming frames will completely die.

JAB Creations

3:49 am on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure if I understand correctly, but you have a song that loads inside a frame and when that frame is reloaded you want it to change information located in another frame?

BINGO! Can JS work live like this?

ironik

4:09 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Yep, what I'd do is do some browser detection then if you're going the frames route build a js function that triggers on the songs onload event.

Your frames will need to have a value in the `name` attribute so javascript can find it easily, then do something like this:

<script>
function changeSong(file)
{
// Where song is the name of the target frame;
parent.song.location.href = file;
}
</script>

Then in the song page's html:
<body onload="javascript:changeSong('song.php?song_id=1')"></body>

That will load the page song.php?song_id=1 into the song frame. I haven't tested it, but it's hopefully fairly straight forward... it should work (actually I'm not sure about the DOM reference parent.. might need to google the correct reference to the frame if it doesn't work).

Also, just some info on the XMLHttpRequest if you wanted to take another route without restricting your site to frames:
[developer.apple.com...]

JAB Creations

4:30 am on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Putting stuff in to the body tag is a no no .... I'm trying to stay with xhtml strict 1.1 on all my pages (cept of course the frames)...

My music pages are loaded in to frame called "mplayer" and my regular pages are loaded in to "content".

ironik

4:41 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Then you'll need a javascript event handler... here's one I use, I've change it a little to suit your needs, although it isn't tested:

<script>
addEvent(window, 'onload', function() {
changeSong();
});

function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent(evType, fn);
return r;
} else {
return false;
}
}
</script>

That should fire a function that calls changeSong() when the page loads... no need to fiddle with the body tag.

JAB Creations

7:14 am on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Neither Firefox or IE worked...didn't print anything out even after a reload...

FF had no JS errors but IE did. This level of JS is a bit above my own head :-D

ironik

10:04 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



Apologies, I haven't had the opportunity to test it out. I know that the add event function works in most browsers, but the part where I've called the changeSong() function is a little last minute, slapped up thingy. I normally use it to attach functions to multiple elements in a page (attach events to input fields etc).

You may have to give up a little of your strict xhtml and go transitional...