I've designed a PHP based chat using mysql tables for database storage, but the obvious problem is that PHP has no obvious way to control the browser like JS does. I'm wondering, are there functions hidden in the depths of PHP which can do generally what JS can do? The main thing I want to do is be able to have the chat update the page only if there are new messages. Currently, it reprocesses which messages it needs to show for each user, every 4 seconds (with a meta refresh tag), which wastes bandwidth and causes annoying side effects which aren't necessary for a chat. While loops don't work for active pages, because they prevent the processing of other pages if you're using frames (I am). I want to avoid JS mainly because the majority of my users are FF users, not IE users, and a good portion of them may disable JS. I've read about the COM object in PHP, which I think allows you to do what JS does, but only for IE. I've also heard of XPCOM for Firefox, but PHP doesn't appear to have any extensions to utilize it. And that still doesn't allow for the near universal acceptance that PHP does. If anyone knows a way to get around these problems with PHP (or some other language which is widely accepted for web pages), it'd be great if you could tell me about them.

Recommended Answers

All 12 Replies

Well, the short answer is No, the long answer is to force them to enable javascript for your page.

Yeah, I ended up going with the AJAX/XML scheme after Shawn replied. Because there appears to be no other way to do it. Question, though... I always get an xml parsing error. I think it's mainly because I haven't changed it to replace my <'s with & lt (or is it & gt?), but I think there's also something special you have to do with links, apparently. Anyone know the easiest way to make links work?

you can use ajax with the mysql chat you are making. its easier than using an xml system.

No, I mean. I'm still pulling the messages from the database, but when I echo them into the xml so that they can be picked up by the xmlhttprequest object, the xml has a parsing error, so the messages don't show. Every single message has a userlink in it, since the chat is tied into a game, as well as some commands having more than one link in them. Other than ampersands and <'s, that's the only thing I need to fix. I just don't quite know how :/

oh, ok. i thought you were using xml files like i did. my question is why are you using xml at all then.

Don't you have to use xml for the xmlhttprequest object to pick it up? I thought you did... If you can do it another way, that'd be awesome, because I'm trying to make the meta version of chat and this one work together, so that people in both chats can interact and have it appear to be one single chat.

no you don't have to use xml with the xmlhttprequest object (weird i know).

post your code so i can show you how to make it work. its kind of hard to explain without knowing how your system is set up.

I can't really post the code, since it has DB structure information, but I can tell you how it works. The JS base of the chat is in an html file which goes in the chat pane, and it grabs new messages/posts new messages from/to a php processing file. The processing file alters the messages to a DB suitable form, inserts them into the DB, then it immediately grabs them back from the db, does some display modifications, and echoes them into an xml variable (the messages have id's in the database, so only the latest ones are echoed). The JS base of the chat then picks up the messages from the xml nodes, echoes them into chat, and re-determines each one as the latest message until there are no more, and it's set to refresh every 3 seconds. Are you saying I could just echo plain html into the php processing file, and it'd pick it up if I adjusted a few things?

yes you could just echo some html from the processing file and use the innerHTML function of javascript to display it in the proper place. make sure you use setInterval to run the javascript every few seconds to update the screen with the new messages.

Thanks, I'll try to make it work. Not 100% familiar with innerHTML, but I'm sure if I get stuck W3schools will have something about it.

thats where i learned about it when i first started using ajax. :) good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.