hi im creating a chat project in php! ive created a database that is used to authorize loggin! im having problem with the chat page itself. on the page therz a textarea and a form and submit button! im confused! i dont know whether to use a txt file to open save update the message or to use mysql! plz help and plz tell me how to automatically refresh page in php! tkx

If I were you I would go for mysql instead of a textfile, both options are possible and mostly a matter of taste. The advantage of mysql is that you can easily request the last 10 or more results (you don't want to display all the conversation every time).

For refreshing you could go with a meta refresh, which is annoying cause if I'm typing something and it meta refreshes, it's gone (atleast, I think. You could test it). You could use an button to let the user manually refresh it when he/she is ready. Still not a very desirable situation.

In my opinion is the best way to go for an in browser chatroom ajax. Ajax allows you to make a request to the server without reloading the page. It's just Javascript with a new object though, so it isn't that hard.

Keep in mind that you will have to let users time out, some users won't press your fancy logout button.

Good luck with your project.

tkx for the advise! i seek advice on ajax then! thank you a lot!

I was wondering if you could do an iframe that will refresh and use css to position it where the you want the conversation to go. Most browsers handle iframes ok but I think that could still become an issue. I don't know if the iframe would be able to inherit the login credentials securely.
Well, I am barely learning php myself so I dont know if it would use the same session.

Well, I also figured that you can use PHP to generate an external javascript file. This way you can create a div area and use the ID and innerHTML property to tack on the messages. This is just a basic example of it and it is of no use without identifying the last datetime and user of the last post.



/* This says to dump as javascript */
Header("content-type: application/x-javascript");

/* This opens a connection */
$connection = mysql_connect(localhost,dbMember,dbPassword)
    or die("document.write(\"Couldn't connect to database!<br />\"");

/* This selects the database for chat log */
$database = "dbContainsChatTable"; //Database containing the tables for the chat logs
$db = mysql_select_db($database,$connection) 
    or die ("document.write(\"Database not selected!\"");

$lastDateTime = $_SESSION['lastMessage'];
$lastMemberPost = $_SESSION['lastWrote'];
/* This gets the latest rows of messages */
/* The query should include a WHERE and column ids to compare against */
/* the last message posted and who posted it so that it only adds new ones */
/* I dont think that the same poster would send more than 1 message a second */
$getChatQuery = "SELECT * FROM chat"; 

$returnsChat = mysql_query($getChatQuery)
    or die("No chatters posting.");
    echo "var chatLog = document.getElementById(\"chatContents\");";
while ($chatMsg = mysql_fetch_array($returnsChat))
    $msgIs = $chatMember.": ".$chatMessage."<br />";
    echo "chatLog.innerHTML=chatLog.innerHTML+\"$msgIs\";\n";
/* This updates the the last message printed */
    $_SESSION['lastMessage'] = $chatDateTime; 
    $_SESSION['lastWrote'] = $chatMember;

Well, it beats having to refresh the page by using javascript to update the the chat messages. Of course getting the javaScript into the site to run is a little more and scrolling when it dumps it if you have the overflow style auto is also pretty neccessary. I use these functions to do it and I also call the scrolling function multiple times to make sure I am at the bottom everytime. The longer the chat message gets, in different browsers the scrolling will leave the 2nd to last message visible. So I call the scrolling function several times to compensate.

function refreshChat() {
    chatUpdate= document.createElement( "script" );
    chatUpdate.src = "./getChat.php";
    head = document.getElementsByTagName( "head" )[0];
    var chatDiv = document.getElementById("chatContents")
    chatDiv.scrollTop = chatDiv.scrollHeight
    return true;
function bottomChat() {
    var chatDiv = document.getElementById("chatContents")
    chatDiv.scrollTop = chatDiv.scrollHeight
    return true;
function chatNewBottom() {
    return true;

It works for me, maybe it will work for you?

This article has been dead for over six months. Start a new discussion instead.