Jump to content

Chat box and IM system


shadowayex

Recommended Posts

Ha! It works in IE7 and Firefox both like before, and I didn't fix the problem with it posting out of order, but using the ORDER BY mysql option I have it displaying in order, which is all that matters. Thank you so much for all your help, both Synook and justsomeguy. I'll give you guys credit on the website once it's up and running. Tomorrow the club and I are getting together to talk about it and it should be getting started soon. If you would like, I'll let you guys take a lok when I get the chat box implemented.

Link to comment
Share on other sites

  • Replies 76
  • Created
  • Last Reply

First, this is AJAX. The first A stands for Asynchronous. You need the third argument to tell Javascript that you want to send an asynchronous request. I don't know what the default value for it is, but you do want it to be asynchronous (or maybe you don't). If it's synchronous that means that Javascript will pause and wait until the response comes back from the server before moving on. If it's asynchronous then it won't wait for the response, it will just handle it whenever it comes in. But instead of just removing the third attribute and relying on whatever the default is, figure out if you want it to be asynchronous or not and put a value in there.For the code, I was thinking something like this:

function getHTTPObject() {  var xhr = false;  if (window.XMLHttpRequest) {	xhr = new XMLHttpRequest();  } else if (window.ActiveXObject) {	try {	  xhr = new ActiveXObject("Msxml2.XMLHTTP");	} catch(e) {	  try {		xhr = new ActiveXObject("Microsoft.XMLHTTP");	  } catch(e) {		xhr = false;	  }	}  }  return xhr;}function get_messages() { var getHttp = getHTTPObject(); getHttp.onreadystatechange = function() {  if (getHttp.readyState == 4) {   document.getElementById("message").innerHTML = getHttp.responseText;  } } getHttp.open("GET", "chat_get_messages.php", true); getHttp.send(null); messages = document.getElementById("message"); messages.scrollTop = messages.scrollHeight;}function send_message() { var postHttp = getHTTPObject(); postHttp.onreadystatechange = function() {  //You want the message box to be instantly updated when they post. Lag less  if (postHttp.readyState == 4) {   document.getElementById("message").innerHTML = postHttp.responseText;  } } postHttp.open("POST", "chat_send_messages.php", true); postHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded"); data = "message=" + document.getElementById("input").value; document.getElementById("input").value = ""; postHttp.setRequestHeader("content-length", data.length); postHttp.send(data); return false;}

You only need 1 function to get the AJAX object. In your other code you didn't have the function return anything, it was only setting a variable.

Link to comment
Share on other sites

I added the third argument as true, works fine. But I was wondering if there was a way to make it so if I'm in the chatroom on one page, and in a different site on the other page, and the other page is in the front, can I make it so it blinks orange when someone posts a message, like messengers do, or make it make a sound?

Link to comment
Share on other sites

You might be able to use window.focus(), I'm not sure.
Is that JavaScript? Point me towards a possible language and I'll figure out the rest. Thank you so much for helping.Also, it puts a "\" in front of apostrophes and quotation marks, and it won't allow "&". Is there any way to fix this? An should used varchar or text for the type?
Link to comment
Share on other sites

Is that JavaScript?
Yes.
Also, it puts a "\" in front of apostrophes and quotation marks, and it won't allow "&".
What does that? In PHP, you'll want to check if magic quotes is enabled and, if so, strip slashes from the input.
Link to comment
Share on other sites

I mean when I send a message. Here's a sample from the chatroom (a friend and I are talking through it)shadowayex(At 2:42 AM on May 21, 2008) : but does it look ok? well, I guess it\'ll depend more on what it\'ll look like once it\'s on the site, because that\'s when I\'ll change the colors and everything - Delete MessageIt puts a slash if front of those things.

Link to comment
Share on other sites

Something is wrong, the chat says:Parse error: parse error, unexpected '{', expecting ')' in /home/www/testtools.freehostia.com/chat_get_messages.php on line 8The code for that page is:

<?php session_start();include("connect.php");$user = $_SESSION['user'];$msginfo = mysql_query("SELECT * FROM players WHERE Username='$user'");$msginfo = mysql_fetch_array($msginfo);$result = mysql_query("SELECT * FROM chat ORDER BY Year, Month, Day, AMorPM, Hour, Minute, Second");while($row = mysql_fetch_array($result)) {   <-----line 8 echo "{$row['Username']}(At {$row['Hour']}:{$row['Minute']} {$row['AMorPM']} on {$row['Month']} {$row['Day']}, {$row['Year']}) : {$row['Message']}"; if($msginfo['AccType']=="adminA") {  echo " - <a href='chatroom.php?action=deletemsg&id=" . $row['MsgID'] . "'>Delete Message</a>"; } echo "<br />";}mysql_close($link);?>

What's wrong?EDIT: Nevermind, it stopped.

Link to comment
Share on other sites

I want to add a few features to my chatroom. The first one is to possibly use AJAX and PHP to make it so when someone starts typing, a message gets sent to a div under the chatbox saying that the user is typing. Can it be done? I figured I could use onkeydown to load the function that did it, but from there I wasn't sure how to write the function.

Link to comment
Share on other sites

You can use this to strip slashes from $_POST:

if (get_magic_quotes_gpc()){  foreach ($_POST as $k => $v)  {	if (is_array($v))	{	  foreach ($v as $i => $j)		$v[$i] = stripslashes($j);	  $_POST[$k] = $v;	}	else	  $_POST[$k] = stripslashes($v);  }}

I figured I could use onkeydown to load the function that did it, but from there I wasn't sure how to write the function.
On the client it should just use AJAX to send a request to indicate that a the user is typing. The PHP script would record the current time for that user, so it would be the last time that they hit a key. When the page sends out a request to get the chat messages it can also get the last time that the user hit a key, and if that's within 5 seconds or so then you can show the message saying that they're typing.
Link to comment
Share on other sites

Um, ok. AJAX to track last key, PHP to record time, right? But how do you use AJAX to track the last key? (Sorry if I'm not getting it, just started AJAX so yeah).Also, I was wanting to add a little option that when you enter the chatroom, it sends a message saying that you joined. And one when you left as well. I tried it myself, but all I succeeded in was getting it to send a message when you joined saying that you joined, then another right after saying you left. Then it does it again when you leave. I copied the AJAX code and was trying to edit it to do what I wanted, but evidently, no success.

Link to comment
Share on other sites

You'll want to use timestamps to keep track of everything. When a key is pressed, someone joins, or someone leaves, the Javascript sends a request to the PHP page indicating which event happened for which user, and PHP will record the time of that event in the database. The next time that the AJAX checks for new messages, PHP will check to see if any events have happened within the past 5 seconds (or whatever you set) and send a list of those events. So when the script checks for new messages it will get the list of messages plus any extra events. That means that your message-getting script can't just write the response to the div. It has to check the response for messages and events, and split them up. You might want to use JSON to build an array in PHP and then send the array as the response. It will make the Javascript code a lot easier.

$response = array();$response['messages'] = array();$response['events'] = array();$response['messages'][] = array('time' => 1182772383, 'user' => 'Bob', 'text' => 'bob was here');$response['events'][] = array('time' => 11823958556, 'user' => 'Bob', 'type' => 'keypress');$response['events'][] = array('time' => 11823958594, 'user' => 'Bob', 'type' => 'exit');echo json_encode($response);

Link to comment
Share on other sites

Oh wow. I've never used timestamps or arrays (aside from the mysql_fetch_array, which builds the array itself and whatnot :)). Maybe I should leave it as is for now and try something else. Because I understand the concept of what I'd do with the arrays and everything, I just don't know how to build them, where to put them, or anything like that. And the only thing I know about timestamps it goes yyyymmddhhmmss in a mysql database. Other than that, I'm stumped.Also, at some point or another, you mentioned something about a session handler (in one of my old questions). I want to use a session handler to keep track of who's online, and I want to be able to query out people who are online so users can have friends lists and see which friends are online or people who are sending PMs back and forth can see if their receiver is still on and whatnot.

Link to comment
Share on other sites

Ok, so I set up the session handler, and got these errors:Warning: mysql_real_escape_string(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/testtools.freehostia.com/sessions.php on line 52Warning: mysql_real_escape_string(): A link to the server could not be established in /home/www/testtools.freehostia.com/sessions.php on line 52Warning: mysql_real_escape_string(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/testtools.freehostia.com/sessions.php on line 54Warning: mysql_real_escape_string(): A link to the server could not be established in /home/www/testtools.freehostia.com/sessions.php on line 54What did I do?
Link to comment
Share on other sites

Ok, I think I might've figured out what I'm screwing up. I see all kinds of variables, and I don't know where they come from. Do I have to set these following variables myself?$save_path, $session_name, $id, $sess_data, $maxlifetime, $minsAnd if so, where should I set them at? In the sessions.php page? (that's where I put the session handler code at) and what data goes in them. I know logically what would go into $id and $mins. I know what the $maxlifetime is asking for, but since I'm using a free hosting site I don't really know how long a session lasts without expiring. The other ones, I don't know. I don't see them set in there, but then again there's about 5% of the code that I figured out what it does, but it's written differently than I'm used to and might do other things as well. I'm pretty sure I have to set those myself, but I need to know where and with what. And I assume it'll need to be before all that code.

Link to comment
Share on other sites

Hmmm, well I did that and set up the table right, but it just gives me those errors. Hmmm, wait. On all my pages I include a file called connect.php that connects to the database. Should I include the sessions.php before or after that?

Link to comment
Share on other sites

When I include the file, whatever pages it's included on, it's like the session doesn't exist. But if I go to page where the file isn't included, the session does. Any reason for this?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...