PDA

View Full Version : PHP dynamic updating a frame!


th83
02-21-2004, 02:10 PM
Well i need a solution!

What i want to do?

some way to update a web page dynamically as the user is browsing it.
For example: in a forum if some one send a private message to other
online user he gets notified immediately about the private message.

I want to update a frame of the web page of all the users visting the page
as some event occur( any time). I am using PHP,a solution in PHP with
some script snippet will be highly appreciated.

regards,


TH

gold_dragon
02-23-2004, 03:53 AM
You need to use these things:[list=a]
JavaScript
PHP (duh? you say)
Hidden Inline Frame or Frame
[/list=a]If you don't know how to use JavaScript and PHP together then I could give you some code that I found. What they do is setup the code and create the JavaScript Variables then when it hits the browser it reads it just like it would if PHP wasn't there. Really neat trick.

There are resources that can show you how to create a popup but I would recommend doing something that is either inside the frame since most people are going to block popups. Or have a message in the inline frame that says they have a new message.

th83
02-23-2004, 11:32 AM
Thanks a lot gold_dragon,

I have follow your advice and using javascipt and PHP.

but i having another problem now. But first look at the code i am writing.

-------------------------------------------------------------------------------------
main.html
-------------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN//3.2">

<HTML>
<HEAD>
<TITLE> Main Page</TITLE>
</HEAD>

<-- notice a zero width left frame! -->
<FRAMESET COLS="0%,*">
<FRAME NAME="left" SRC="left.html" SCROLLING="AUTO">
<FRAME NAME="right" SRC="right.html" SCROLLING="AUTO">
</FRAMESET>

</HTML>

-------------------------------------------------------------------------------------
left.html
-------------------------------------------------------------------------------------
<html>
<head>

<title>Auto Reload</title>

<script language="JavaScript">
<!--
var time = null
function move()
{
window.location = 'left.html'
}
//-->
</script>

</head>

<-- refresh the page every 5 seconds -->
<body onload="timer=setTimeout('move()',5000)">

<%php

// PHP code to query the database and find out
// if some event has occur or not

%>

</body>

-------------------------------------------------------------------------------------
right.html
-------------------------------------------------------------------------------------
<HTML>

<HEAD>

<TITLE> Page i really want to update </TITLE>

</HEAD>

<BODY>

<-- !!!!!!!I want to udate some text here!!!!!!! -->

</BODY>
</HTML>


I a have made the width of "left" frame 0% because i do not want to display this ungly flickering page to the user.

Now the problem is that i want to update some text in "right" frame (as the comment has mention).

I can not find any way in Javascript (nor PHP) that runs in left frame when it is refresh and update the right frame if some events has occur!!

please look in to this problem!

regards,

TH

stuka
02-23-2004, 12:43 PM
'Updating' a frame is as simple as using the 'target' attribute - I'm no javascript guru, so I can't tell you off the cuff how to do it, but you need to put the fact that the target = 'name of right frame' in your calling code.

gold_dragon
02-23-2004, 02:06 PM
When I said use JavaScript and PHP together, I meant:<?php
echo "<script language=\"JavaScript\">";
echo "var new_message;";
// Connect to database and query the table

// Process to see if new message has occured

if($new_message)
{
echo "new_message = true;";
} else {
echo "new_message = false;";
}
?>
function move()
{
if(new_message == true)
{
window.target(); // something or other on along these lines
/* Actually, you just want to pass by POST that a new
message has occured then refresh the frame to display
the message. I don't know enough of JavaScript to know
how do to this.
*/
}
window.location = 'left.html'
}
//-->
</script>
If worse comes to worse then you can always use the get method and have the infomation in the url, since the viewer isn't going to see that information anyway then there shouldn't be anything wrong with it. Even if they can it shouldn't matter.

darelf
02-23-2004, 02:31 PM
Why does trying to keep a webpage apprised of asynchronous events in real time seem like a really, really, really.... bad idea?

Is it just not desirable to have some small little java applet do this function? I know I don't like java in my websites, but if you absolutely must have this functionality, isn't this *precisely* what java applets are supposed to solve?

gold_dragon
02-23-2004, 04:01 PM
Yeah, but applets do take time to download and for connecting to a database you will need to use the Enterpise Edition and the server may or may not allow that. There is rowset that allows this but I wouldn't count to much on it. This is also one of the reasons why I haven't implemented something like this on my web site. Just not feasible with the low amount of visitors that I recieve. Also, who cares if they have a message at that time, they probably aren't going to look at it anyway. I can be good if like, they are looking at a large document or playing a game such as Java or Flash and you wish to get their attention. This would sort of create an instanteous messaging service for the web site but the chances that the user being on at that given time and the other person being on at the same time is slim unless the site is very popular but still. It is would almost be better to wait until the page reloads to check to see if there is a new message.

Also, refreshing a page isn't exactly all that nice to do to the person and the server. Doing that many queries in a given day is really bad. Even more so as the number of visitors increases.

th83
02-24-2004, 06:21 AM
There is no perfect solution in the world!

There are trade offs in every thing.

Well so far I have find only to way to periodically updating a page in the browser on is applet (or flash animation) and other is to refresh a 0% width frame.

Many people do not like applet first it require a Java Enable browser secondly they say a foreign program is downloaded in our PC and then it runs!!!

Thats why i choose the page refeshing solution. Well yes it take lot of band width. but if the interval is big say 25 to 30 second then i think there is not much performance hit.

what you all say?

regards,

TH

gold_dragon
02-24-2004, 08:41 AM
I say that since the page you are refreshing is small, has no pictures and is just querying a database table to see if there is any new messages then go for it. The interval is a good one since it may take that long for the script to run. It is also (should be if not) the maximum PHP will run before it stops automatically. This is good if you accidently have a infinite loop... like I did a while back but nevermind.

If you are going to have something like this then you may want to have give the option to the member to have the frame shutoff. It may freak the visitor out and or annoy the visitor if they get a lot of private messages. I'm just talking about the worse case, it is unlikely that something like this would happen but who knows your site may get popular or maybe someone is an admin and a lot of people have questions.

th83
02-24-2004, 09:04 AM
good idea!

thanks

TH

th83
02-24-2004, 11:37 AM
I found how to update a one frame while in other frame.

I had not clear concept of Document Object Model(DOM) of JavaScript.
but now after a reading the DOM in detail it was just easy.

To help others how are looking for something similar i am posting some code.

-----------------------------------------------------------------------
main.html
-----------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN//3.2">

<HTML>
<HEAD>
<TITLE> Main Page</TITLE>
</HEAD>


<FRAMESET COLS="0%,*">
<FRAME NAME="left" SRC="left.html" SCROLLING="AUTO">
<FRAME NAME="right" SRC="right.html" SCROLLING="AUTO">
</FRAMESET>

</HTML>

---------------------------------------------------------------------
left.html
---------------------------------------------------------------------
<html>
<head>

<title>Auto Reload</title>

<?php

echo "<script language=\"JavaScript\">";
echo "var new_message;";

// Connect to database and query the table

// Process to see if new message has occured

if($new_message)
{
echo "new_message = true;";
}
else
{
echo "new_message = false;";
}

?>

function move()
{
if(new_message == true)
{

top.frames[1].document.getElementById('TextToUpdate').innerHTML = "You got a new message";
}

window.location = 'left.html'
}

//-->
</script>

</head>


<body onload="timer=setTimeout('move()',25000)">


</body>

---------------------------------------------------------------------
right.html
---------------------------------------------------------------------
<HTML>

<HEAD>

<TITLE> Page i really want to update </TITLE>

</HEAD>

<BODY>




<a id="TextToUpdate"> </a>

</BODY>
</HTML>

-----------------------------------------------------------------------

PS: if some one still do not get it then i will recommend to read some
thing about DOM of javascript.


i hope there are no typos this time!!

thanks again for helping.

regards,


TH

gold_dragon
02-24-2004, 02:25 PM
Yeah, DOM isn't hard but I just haven't looked at it yet. Not really high in my lists of stuff to do when I'm doing C++, Java, and Perl.

Now all, you have to do is connect to the database and figure out if there is a new message. I would think placing a timestamp field in the table and calculating if the message is new or not would be the best option. Of course, since you check every 25 seconds, you have have a certain lag and would have to make sure you have that in there also.