Jump to content
MakeWebGames

Ajax Chat


Scorpio

Recommended Posts

If you have Ajax Gang/Channel Chat System by Dave Macaulay. I have interigated it with another chat by Dayo.

There are 5 files altogether.

But I have a problem. On the chat only the userid shows not the username, may someone help me out please. Recieve is the file that needs to be edit to show the username, I tried my best to make it show but I couldn't do it.

 

ajch.css

 

#errordivision {
position: absolute;
top: 40%;
left: 35%;
height: 170px;
width: 300px;
border: 2px solid;
}
#chatresp {
border: 1px solid;
background-color:#777777;
overflow: auto;
height: 400px;
width: 400px;
position: relative;
font-family: Arial,Verdana,Helvetica,sans-serif,monospace;
font-size: 12px;
}

 

chat.php

 

<?php
include 'globals.php';


// the staff functions
if ($ir['user_level']>1) {

// Delete post
if ($_GET['action']=='del' && isset($_GET['chat_id'])) {
mysql_query("DELETE FROM `chat_box` WHERE `chat_id`=".abs(intval($_GET['chat_id'])));
}

} else {echo 'Nope that dont work!';}



?>

<script type="text/javascript" src="send.js"></script>
<link rel="stylesheet" type="text/css" href="ajch.css">
<form name="chatform" onSubmit="send();return false;">
<!--<textarea name="chatresp" cols="50" rows="10" id="chatresp" readonly="readonly"></textarea><br>-->
<div id="chatresp" name="chatresp"></div>
<input type="hidden" name="guestname" id="name" size="7"> <input type="text" name="chatext" style="width:325px" id="chatext"><input type="submit" value="Send" style="width:75px" onClick="send();return false;">
</form>
<div style="width:400px; text-align:left"><small style="color:red;"><b>[bB-Code ~ Enabled]</b></small></div>

<?php
$h->endpage();
?>

 

recieve.php

 

<?php

session_start();
$userid=$_SESSION['userid'];
include "config.php";
define("MONO_ON", 1);
require "class/class_db_{$_CONFIG['driver']}.php";
require_once('global_func.php');
$db = new database;
$db->configure($_CONFIG['hostname'], 
$_CONFIG['username'],
$_CONFIG['password'], 
$_CONFIG['database'], 
$_CONFIG['persistent']);
$db->connect();


$is=$db->query("SELECT * FROM `users` WHERE `userid`='".$userid."'");


$ir=$db->fetch_row($is);


// Mass Delete posts after X hours X mins X seconds
$time=time() - (24*60*60);
mysql_query("DELETE FROM `chat_box` WHERE `chat_time`<".$time);

class bbcode {

   function code_box($text)
   {
       $output = "<div class=\"code\"><h6>Code</h6>\\1</div>";

   return $output;

   }

   function quote($text)
   {
       $output = "<blockquote><h6>Quote:</h6>\\1</blockquote>";

   return $output;

   }

   function htmlout($text)
   {
       $text = stripslashes($text);
       $text = htmlspecialchars($text);    
       $text = nl2br($text);

       return $text;
   }


   function parse($text)  
   {
       // First: If there isn't a "[" and a "]" in the message, don't bother.
       $text = " " . $text;
       if (! (strpos($text, "[") && strpos($text, "]")) )
       {
           return $text;    
       }
       else
       {
           // de uiteindelijke code
           $text = $this->htmlout($text);

             $text = preg_replace("/\\[b\\](.+?)\[\/b\]/is",'<b>\1</b>', $text);
             $text = preg_replace("/\\[i\\](.+?)\[\/i\]/is",'<i>\1</i>', $text);
             $text = preg_replace("/\\[u\\](.+?)\[\/u\]/is",'<u>\1</u>', $text);
             $text = preg_replace("/\[s\](.+?)\[\/s\]/is",'<s>\1</s>', $text);

	  // Un advised bb code we advise you dont use the folowing bb code if you want to use it use at your own risk  
         //  $text = preg_replace("/\[code\](.+?)\[\/code\]/is","".$this->code_box('\\1')."", $text);
         //  $text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","".$this->quote('\\1')."", $text);
         //  $text = eregi_replace("\\[img]([^\\[]*)\\[/img\\]","<img src=\"\\1\">",$text);  
         //  $text = eregi_replace("\\[size([^\\[]*)\\]([^\\[]*)\\[/size\\]","<font size=\"\\1px\">\\2</font>",$text);

             $text = eregi_replace("\\[color=([^\\[]*)\\]([^\\[]*)\\[/color\\]","<font color=\"\\1\">\\2</font>",$text);    
           return $text;
       }    
   }
}
$bbcode = new bbcode;
$q = mysql_query("SELECT `chat_id` FROM `chat_box` ");
$num=mysql_num_rows($q);
if ($num < 26) {
$limit=$num; $num = 0; 
} else {$num = $num-26; $limit=26;}
$q = mysql_query("SELECT * FROM `chat_box` ORDER BY `chat_id` ASC LIMIT ".$num.", ".$limit);



echo '<div align="left">';
while ($c=mysql_fetch_array($q)) {

if ($ir['user_level'] > 1) {$staff='[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ';} else {$staff='';}
echo ' '.$staff.'<strong>'.ucwords($c['chat_user']).':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'<br>';
}

echo '</div>';
?>

 

send.php

 

<?php

session_start();
$userid=$_SESSION['userid'];
include "config.php";
define("MONO_ON", 1);
require "class/class_db_{$_CONFIG['driver']}.php";
require_once('global_func.php');
$db = new database;
$db->configure($_CONFIG['hostname'], 
$_CONFIG['username'],
$_CONFIG['password'], 
$_CONFIG['database'], 
$_CONFIG['persistent']);
$db->connect();



$is=$db->query("SELECT * FROM `users` WHERE `userid`='".$userid."'");
$ir=$db->fetch_row($is);

$text = mysql_real_escape_string($_GET['msg']);
$name = $ir['username'];

global $db,$ir,$c,$h,$userid;


if ($_GET['msg'])
{
$db->query("INSERT INTO chat_box(`chat_user`,`chat_channel`, `chat_text`, `chat_time`) VALUES ('$userid','1',' ".$text."',unix_timestamp())");
}
echo 'Sending ...';
?>

 

send.js

 

function err(text){
body = document.body;
var errdiv = document.createElement("div");
errdiv.id = "errordivision";
errdiv.className = "errordivision";
errdiv.innerHTML = text;
body.appendChild(errdiv);
setTimeout("document.getElementById('errdiv').style.display = 'none'", 3000);
}
function makeRequest(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
} else if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
err("You browser doesn't support XMLHttpRequestObject");
}
}
var req = makeRequest();
function send(){
if(req.readyState == 4 || req.readyState == 0){
req.open("GET", "send.php?name="+document.chatform.guestname.value+"&msg="+document.chatform.chatext.value);
req.send(null);
}
document.chatform.chatext.value = '';
}
function recieve(){
req.open("GET", "recieve.php");
req.onreadystatechange = handleResp;
req.send(null);
}

function handleResp(){
if(req.readyState == 4 && req.status == 200){
document.getElementById("chatresp").innerHTML = req.responseText;
var elem = document.getElementById("chatresp");
elem.scrollTop = elem.scrollHeight;
}
}
function init(){
var num = parseInt(Math.random()*10);
var stnum = String(num);
var num1 = parseInt(Math.random()*10);
var stnum1 = String(num1);
var num2 = parseInt(Math.random()*10);
var stnum2 = String(num2);
var num3 = parseInt(Math.random()*10);
var stnum3 = String(num3);
document.chatform.guestname.value = "guest"+stnum+stnum1+stnum2+stnum3;
setInterval('recieve()', 2000);
recieve();
}
window.onload = init;

 

[ATTACH=CONFIG]936[/ATTACH]

 

Sorry this is for V2

screeny.png.c04462747cb725b64134cdceb8ddd21d.png

Edited by Scorpio
Link to comment
Share on other sites

Try something like this though it may be a bit sloppy thinking but hey....

function getUser($id)
{
global $db;
$result = $db->query("select username from users where userid = {$id}");
if ($db->num_rows($result))
return $result["username"];
}

I hope that helps as its not tested and im on my phone but you get the idea right?

Then in receive do this:

getUser($c["chat_user"])
Edited by KyleMassacre
usage info
Link to comment
Share on other sites

In recieve.php

find

while ($c=mysql_fetch_array($q)) {

if ($ir['user_level'] > 1) {$staff='[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ';} else {$staff='';}
echo ' '.$staff.'<strong>'.ucwords($c['chat_user']).':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'';
}

echo '</div>';

 

Replace with

while ($c=mysql_fetch_array($q)) {
$name = $db->fetch_single($db->query(sprintf("SELECT `username` FROM `users` WHERE (`userid` = %u)", $c['userid'])));
if ($ir['user_level'] > 1) {$staff='[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ';} else {$staff='';}
echo ' '.$staff.'<strong>'.ucwords($c['chat_user']).' '. ($name).':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'';
}

echo '</div>'; 
Link to comment
Share on other sites

In recieve.php

find

while ($c=mysql_fetch_array($q)) {

if ($ir['user_level'] > 1) {$staff='[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ';} else {$staff='';}
echo ' '.$staff.'<strong>'.ucwords($c['chat_user']).':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'';
}

echo '</div>';

Replace with

while ($c=mysql_fetch_array($q)) {
$name = $db->fetch_single($db->query(sprintf("SELECT `username` FROM `users` WHERE (`userid` = %u)", $c['userid'])));
if ($ir['user_level'] > 1) {$staff='[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ';} else {$staff='';}
echo ' '.$staff.'<strong>'.ucwords($c['chat_user']).' '. ($name).':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'';
}

echo '</div>'; 

Probably should be $c['chat_user'] rather than $c['userid'] in that query. ;)

Link to comment
Share on other sites

receive.php from line 86 forward, try this:

   $q = mysql_query("SELECT `chat_id` FROM `chat_box` ORDER BY `chat_id` ASC LIMIT 26");
   echo '<div align="left">';
   while ($c=mysql_fetch_array($q)) {
       $staff = ($ir['user_level'] > 1) ? '[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ' : '';
       echo ' '.$staff.'<strong>'.$c['chat_user'].':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'';
   }
   echo '</div>';
Link to comment
Share on other sites

Hello, thank you guest for the help but that didnt work, so as lithuim. Thanks everybody but I have fixed it!

 

<?php

session_start();
$userid=$_SESSION['userid'];
include "config.php";
define("MONO_ON", 1);
require "class/class_db_{$_CONFIG['driver']}.php";
require_once('global_func.php');
$db = new database;
$db->configure($_CONFIG['hostname'],
$_CONFIG['username'],
$_CONFIG['password'],
$_CONFIG['database'],
$_CONFIG['persistent']);
$db->connect();



$userid = $_SESSION['userid'];
$is = $db->query("SELECT * FROM `users` WHERE `userid` = ".$userid) or die(mysql_error());
$ir = $db->fetch_row($is);

function getUsername($userid) {
   global $db;
   $select = $db->query("SELECT `username` FROM `users` WHERE `userid` = ".$userid);
   if($db->num_rows($select)) {
       $user = $db->fetch_row($select);
       return $user['username'];
   }
}

// Mass Delete posts after X hours X mins X seconds
$time=time() - (24*60*60);
mysql_query("DELETE FROM `chat_box` WHERE `chat_time`<".$time);

class bbcode {

   function code_box($text)
   {
       $output = "<div class=\"code\"><h6>Code</h6>\\1</div>";

   return $output;

   }

   function quote($text)
   {
       $output = "<blockquote><h6>Quote:</h6>\\1</blockquote>";

   return $output;

   }

   function htmlout($text)
   {
       $text = stripslashes($text);
       $text = htmlspecialchars($text);    
       $text = nl2br($text);

       return $text;
   }


   function parse($text)  
   {
       // First: If there isn't a "[" and a "]" in the message, don't bother.
       $text = " " . $text;
       if (! (strpos($text, "[") && strpos($text, "]")) )
       {
           return $text;    
       }
       else
       {
           // de uiteindelijke code
           $text = $this->htmlout($text);

             $text = preg_replace("/\\[b\\](.+?)\[\/b\]/is",'<b>\1</b>', $text);
             $text = preg_replace("/\\[i\\](.+?)\[\/i\]/is",'<i>\1</i>', $text);
             $text = preg_replace("/\\[u\\](.+?)\[\/u\]/is",'<u>\1</u>', $text);
             $text = preg_replace("/\[s\](.+?)\[\/s\]/is",'<s>\1</s>', $text);

	  // Un advised bb code we advise you dont use the folowing bb code if you want to use it use at your own risk  
         //  $text = preg_replace("/\[code\](.+?)\[\/code\]/is","".$this->code_box('\\1')."", $text);
         //  $text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","".$this->quote('\\1')."", $text);
         //  $text = eregi_replace("\\[img]([^\\[]*)\\[/img\\]","<img src=\"\\1\">",$text);  
         //  $text = eregi_replace("\\[size([^\\[]*)\\]([^\\[]*)\\[/size\\]","<font size=\"\\1px\">\\2</font>",$text);

             $text = eregi_replace("\\[color=([^\\[]*)\\]([^\\[]*)\\[/color\\]","<font color=\"\\1\">\\2</font>",$text);    
           return $text;
       }    
   }
}
$bbcode = new bbcode;
$q = mysql_query("SELECT `chat_id` FROM `chat_box` ");
$num=mysql_num_rows($q);
if ($num < 30) {
$limit=$num; $num = 0; 
} else {$num = $num-26; $limit=26;}
$q = mysql_query("SELECT * FROM `chat_box` WHERE `chat_channel`='1' ORDER BY `chat_id` ASC LIMIT ".$num.", ".$limit);





echo '<div align="left">';








echo '<div align="left">';
while ($c=mysql_fetch_array($q)) {





if ($ir['user_level'] > 1) {$staff='[<a href="chat1.php?action=del&chat_id='.$c['chat_id'].'">X</a>] ';} else {$staff='';}
echo ' '.$staff.'<strong>

'.getUsername($c['chat_user']).':</strong> '.$bbcode->parse(htmlspecialchars($c['chat_text'])).'<br>';
}

echo '</div>';






?>
Link to comment
Share on other sites

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.   Paste as plain text instead

  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...