Jump to content

AJAX Chat - Need Some Help!!


Scotticus

Recommended Posts

<?php
class chat{
var $host="hosthere";
var $user="userhere";
var $db="dbnamehere";
var $pass="dbpasswordhere";

//While also "sending" messages to the database this function also reads your chatroom table, it will "disect" the returned array and find every column and row so there is no need to change this at all
function connect_easy($query)
	{
		$b = array();
		if(!$connect = mysql_connect($this->host,$this->user,$this->pass));
		if(!$dbr = mysql_select_db($this->db));
		if(!($result = mysql_query($query)));
		@$num = mysql_num_rows($result);
		@$num2 = mysql_num_fields($result);
		for($x=0;$x<$num;$x++)
		{
			$a = mysql_fetch_array($result);
				for($i=0;$i<$num2;$i++)
				{
					$b[$x][$i] = html_entity_decode($a[$i]);
				}
		}
		return $b;
	}


function show($a){
	if(count($a)>0){
		$a=array_reverse($a);
		if(count($a)<38)
			$end=count($a);
		else
			$end=38;
		for($i=0;$i<$end;$i++)
		{
			//$a[$i][0] would reffer to the first field in your chatroom table, change these to correspond to what information the script needs to know about your table. My columns are (as read left to right) id, username, userlevel, race, message, to, date and are depicted as such by the numbers 0-6, i hope you can understand that ^^
			$id = $a[$i][0];
			$username = $a[$i][1];
			$userlevel = $a[$i][2];
			$race = $a[$i][3];
			$message = $a[$i][4];
			$to = $a[$i][5];
			$date = $a[$i][6];

			$message = eregi_replace('"', "'", $message);
			$message = eregi_replace("<", "<", $message);
			$message = eregi_replace(">", ">", $message);	//Clean the message

			if($userlevel == "1")	//Admin
			{
				if(substr($message, 0, 2) == "/e")	//Sent an Emote message
				{
					$message = substr($message, 3);
					$message = "<i><b><a href='javascript:toptell(\"$username\");'><font color='green' style='text-decoration:none'>".$username."</a></a> ".$message."</font></b></i><br />";
					echo $message;
				}
				else	//Normal Message
				{
					echo("<b><a href='javascript:toptell(\"$username\");'><font color='red' style='text-decoration:none'>".$username."</font></a></b><font color='silver'> says:</font> <font color='#FFFFFF'>".$message."</font><br />");
				}
			}
			elseif($userlevel <= "3")		//Player or Mod
			{
				if(substr($message, 0, 2) == "/e")
				{
					$message = substr($message, 3);
					$message = "<i><a href='javascript:toptell(\"$username\");' target='_parent'><font color='aqua' style='text-decoration:none'>".$username."</a> ".$message."</font></i><br />";
					echo $message;
				}
				else
				{
					echo("<a href='javascript:toptell(\"$username\");' target='_parent'><font color='aqua' style='text-decoration:none'>".$username."</font></a><font color='silver'> says:</font> <font color='#FFFFFF'>".$message."</font><br>");
				}
			}	//Elseif everything else you need to...
		}
	}
}
function main(){
	session_start();
	echo '<html><head><script language="JavaScript">
			function createRequestObject(){
		var request_;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
		 request_ = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
		 request_ = new XMLHttpRequest();
		}
		return request_;
		}

		var http = new Array();
		var http2 = new Array();


		function getInfo(){

		var curDateTime = new Date();
		http[curDateTime] = createRequestObject();

		http[curDateTime].open(\'get\', \'refresh.php\');

		http[curDateTime].onreadystatechange = function(){
			if (http[curDateTime].readyState == 4) 
	    	{
	        	if (http[curDateTime].status == 200 || http[curDateTime].status == 304) 
	        	{
	           	 	var response = http[curDateTime].responseText;
	 				document.getElementById(\'view_ajax\').innerHTML = response;
	        	}
	    	}
		}

		http[curDateTime].send(null);
		}


		function getInfo2(){
		var curDateTime = new Date();
		http2[curDateTime] = createRequestObject();
		http2[curDateTime].open(\'get\', \'submit.php?chat=\'+ document.ajax.chat.value);
		http2[curDateTime].send(null);
		}

		function send(){
		getInfo2();
		document.ajax.chat.value="";
		}


		function refreshchat(){
		getInfo();
		window.setTimeout("refreshchat()", 8000);
		}

		function toptell( cName ){
			parent.ajax.chat.focus();
			if (parent.ajax.chat.value == "") 
			{
				parent.ajax.chat.value="/m " + cName + ": ";
				parent.ajax.chat.focus();
			} 
			else if (parent.ajax.chat.value == "/m " + cName + ": ") 
			{
				parent.ajax.chat.value="/id " + cName;
				parent.ajax.chat.focus();
			}
			else
			{
				parent.ajax.chat.value="/m " + cName + ": ";
				parent.ajax.chat.focus();
			}
		}
		</script>
		</head>
		<body bgcolor="#000000" onLoad="refreshchat()">
		<form action="JavaScript: send()" method="get" name="ajax">
		<td width="78%"><div align="left"><input type="text" name="chat" size="78%" maxlength="255" class="input">
		<input type="button" class="input" onClick="send()" value="-Send-"></td>
                        <td width=5%><form>
                        </td>
                        </select>
                        <br><br>
		<div id="view_ajax" style="overflow=auto; width: 100%; height: 600px; border: 0px; font-family: arial; font-size: 10pt; align="left">
			<center><--- Chat is now preparing, please wait ---></center>
		</div>
		</form>
		</body></html>';
	}
}

 

edit wrapped the code tags around the code

Link to comment
Share on other sites

Now that is chat.php, There are other files.. Refresh.php, Chatroom.php + Submit.php

 

All of this is online.. And on my server.. It works.. Directly from the database.. But not from Chatroom.php ..

 

If you want the code posting for those also.. Please ask.

 

Thanks

 

Scott

 

edit removed bold tags

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.