View Full Version : Page Refresh
InNeedofHelp
06-24-2006, 08:10 PM
Is there anyway to refresh a page based on user input using php? or am i looking at javascript? Basically, i have a chatroom, and i don't want to use a meta tag because i want the page to refresh faster than every 5 or 1 seconds if it has to, or as slow as it has to, based on when any of the users sends a message. For example, 4 people are in my chatroom, one of them sends a message, and each of the 4 peoples page refreshes. Is that even possible using PHP or javascript?
Thanks.
P.S.: Sorry if this is in the wrong forum. I figured Javascript was strictly browser based and wouldn't be able to update each active user's page, and therefore i posted in here.
djr33
06-24-2006, 08:15 PM
No. PHP is not realtime.
And, no. Javascript cannot access the server.
The ONLY way to do what you are asking is using AJAX.
More complex, but it's worth looking into.
InNeedofHelp
06-24-2006, 08:20 PM
Oy, I was afraid of that. Hehe. I'm busy learning PHP, and all the while I'm learning more Javascript (it seems like to learn all of javascript would take an entire lifetime). I didn't want to have to get into AJAX too haha. Ah well, any idea where I can find a good AJAX tutorial to help me with this problem?
Thanks.
djr33
06-24-2006, 08:25 PM
Not sure. I know WHAT AJAX can do, not so much HOW it does it ;)
It is all javascript based, though.
You send a request for a page, using javascript. That is "AJAX".
Then you'd control that process using javascript, ifs, loops, user input, whatever.
Not TOO complex... but certainly advanced.
The server cannot initiate a connection to the client. The client must send a request before any data can be sent by the server. This means that you have no way of telling when there is a new message available. You simply have to refresh periodically. Through trial and error with people of different connection speeds, I find five seconds is probably the best interval.
djr33
06-24-2006, 08:59 PM
The only catch is that using AJAX, you can "refresh" every 5 seconds, but only actually refresh the text if it varies from what it was before.
That's a very nice trick, and looks a lot better than flashing text.
But, exactly... the server can't initiate anything. That's the hard part about coding chatrooms, etc.
InNeedofHelp
06-24-2006, 09:34 PM
Hm. So I'm probably better off just using a meta tag?
You're better off using XMLHttpRequest, then falling back on a meta tag. Check out the PHP/MySQL chat (http://japantown.awardspace.com/generalchat.php?frames) I wrote for Johnnymushio. I hope he won't mind my using it as an example, I've just moved to a new host and I don't have everything up yet. :)
djr33
06-25-2006, 01:01 AM
Twey, would you consider releasing that? I'd certainly love a copy :)
InNeedofHelp
06-25-2006, 03:15 AM
What is XMLHttpRequest? I went to that link you gave me, but I don't know exactly what i'm looking for there. :confused:
Is this XML thing the best answer to my problem?
djr33
06-25-2006, 06:59 AM
XMLHttpRequest IS AJAX.
It's the way that you "request" a page through javascript, behind the scenes, then it can do stuff with the contents of that page, like showing it, replacing part of the page, etc etc.
InNeedOfHelp: View the source of the messages frame.
djr33: It's been released for a long time :) See http://www.twey.co.uk/dd/chat.phps
djr33
06-25-2006, 10:10 PM
And I can use that on my site?
Great.
Did I miss when you submitted this to DD? If you haven't, I suggest you should, unless you're wanting to keep it yourself, in which case I understand, but I am sure it would be a big help to people :)
Thanks!
EDIT: Ah, darn. It's trying to parse the page, and giving a weird javascript error "<?php echo" something or other.
Use .txt, I guess. .phps was so nice before, though, with the color coding...
I didn't submit it because the interface layout (the top bit, at least) belongs to Johnnymushio. Also, he claims there are bugs when using multiple instances with the same database, although I've tried it myself and am unable to reproduce them.
EDIT: Ah, darn. It's trying to parse the page, and giving a weird javascript error "<?php echo" something or other.
Use .txt, I guess. .phps was so nice before, though, with the color coding...Yeah, unfortunately my new host doesn't parse phps. It sends it as text/plain, though, I think, so your browser shouldn't have a problem unless it's IE.
djr33
06-26-2006, 12:47 AM
IE.... or Safari, it seems. Weird. Mozilla works, though.
Thanks :)
Hey... wait... it uses my pixel color thing too? :)
(and, with that, this is now officially off topic :p)
EDIT: I think I also need the info as to what to set up in the database. I can look more later if you don't have the time to check for me.
Oh yeah, I forgot it had your pixel-colour thing :)
I've an SQL dump somewhere...
-- MySQL dump 10.9
--
-- Host: localhost Database: twey_japantown
-- ------------------------------------------------------
-- Server version 4.1.20-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `chat_messages`
--
DROP TABLE IF EXISTS `chat_messages`;
CREATE TABLE `chat_messages` (
`id` int(11) NOT NULL auto_increment,
`msg` text,
`user` int(11) default NULL,
`time` datetime default NULL,
`special` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Table structure for table `chat_users`
--
DROP TABLE IF EXISTS `chat_users`;
CREATE TABLE `chat_users` (
`id` int(11) NOT NULL auto_increment,
`handle` text,
`font` text,
`lastseen` datetime default NULL,
`title` text,
`logged` tinyint(1) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40000 ALTER TABLE `chat_users` ENABLE KEYS */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
djr33
06-26-2006, 07:12 AM
Well.... got it running... now to fix the layout and setup the login to coincide with my forum... :)
Thanks!
Back on topic now, maybe. Heh.
InNeedofHelp
06-26-2006, 05:31 PM
http://www.twey.co.uk/dd/chat.phps
^That script is incredible. When i look at that and then look at my chat script it gives me a good laugh.
The XMLHttpRequest looks kind of complicated, but I'll look into it.
While we're on the topic of chatrooms, do either of you have any idea how i would color each persons name separately? Like in AOL Instant Messanger, the color of each persons name is different to kind of keep orientation. How do i do that in PHP?
Thanks.
That script is incredible.It's not actually all that awesome. It's kind of messy; I've just copied and pasted a lot of code where it wasn't strictly necessary, and the whole setupDatabase() thing isn't needed at all. I was originally going to only call it where a database connection was needed, for efficiency, but with the introduction of the little keep-alive thing it ended up kind of redundant.
While we're on the topic of chatrooms, do either of you have any idea how i would color each persons name separately? Like in AOL Instant Messanger, the color of each persons name is different to kind of keep orientation. How do i do that in PHP?Again, study the script -- it offers users the choice of their text colour.
djr33
06-26-2006, 05:37 PM
There's a cool part of Twey's chatroom (I wrote this part, so I particularly like it) where a user can click on an image (as in jpg) and the color value at that pixel will be outputted and they can use that as their color.
As for rotating through colors... that's a list, and based on who entered first, etc.
Really, it's quite complex to set that up, for no big reason... the colors usually annoy me in AIM. It's also based on each user... not everyone sees the same colors... so it's based on you as red, the next person blue, etc etc., but individual for each person. It would be a whole different aspect to code, and doesn't seem like it's really worth doing that much for it.
InNeedofHelp
06-28-2006, 02:45 PM
Allright.
Thanks. :D
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.