Chat room is not working need help

Chat room is not working need help

Page 1 of 1

1 Replies - 1484 Views - Last Post: 02 July 2009 - 01:12 PM

#1 colorodo13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 16-February 09

Chat room is not working need help

Post icon  Posted 02 July 2009 - 12:11 PM

Error messages I am receiving
1. 'responseFunc' is undefined - line 1 of ajax.js file
2. Expected '}' - chatbox.js file
3. Object expected in chat.html file, Code:0, URI:http://www.globalconferencing.net/chat%20box/chat.html

To further give another example when I test that chat I enter a user name hit sign in and nothing happens. The user name is not being posted anywhere and the message is doing nothing also.... Can somebody please help...

Here is the code for Chat.html
<html>
<link rel="stylesheet" type="text/css" href="cb_style.css">
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="chatbox.js"></script><html>
<head>
<title>Welcome to 1888vcrooms.com Chat Room</title>
</head>

<body onbeforeunload="signInForm.signInButt.name='signOut';signInOut()" onload="hideShow('hide')">

<h1>1888vcrooms.com Chat Room</h1>
<form id="signInForm">
	<input id="userName" type="text">
	<input id="signInButt" name="signIn" type="submit" value="Sign in">
	<span id="signInName">User name</span>
</form>

<div id="chatBox"></div>

<div id="usersOnLine"></div>

<form id="messageForm">
	<input id="message" type="text">
	<input id="send" type="submit" value="Send">
<div id="serverRes"></div>
</form>
</body>
</html>



Here is the code for ajax.js
Ajax_Send("POST","my_url","name1=ahmad&name2=511",reponseFunc);



Here is the code for chatbox.js
function hideShow(hs){
	if(hs=="hide"){
		signInForm.signInButt.value="Sign in"
		signInForm.signInButt.name="signIn"
		messageForm.style.display="none"
		signInForm.userName.style.display="block"
		signInName.innerHTML=""
		}
		if(hs=="show"){
			signInForm.signInButt.value="Sign out"
			signInForm.signInButt.name="signOut"
			messageForm.style.display="block"
			signInForm.userName.style.display="none"
			signInName.innerHTML=signInForm.userName.value 
			}
			}
			// Sign in and Out
			function signInOut(){
				// Validationif (signInForm.userName.value=="" || signInForm.userName.value.indexOf(" ")>-1)
				{alert("Not valid user name\nPlease make sure your user name didn't contains a space\nOr it's not empty.");
				signInForm.userName.focus();
				return false;
				}
				// Sign in
				if (signInForm.signInButt.name=="signIn"){
					data="user=" + signInForm.userName.value +"&oper=signin"
					Ajax_Send("POST","users.php",data,checkSignIn);
					return false
					}
					}
					// Sign in response
					function checkSignIn(res){
						if(res=="userexist"){alert("The user name you typed is already exist\nPlease try another one");
						return false;
						}
						if(res=="signin"){
							hideShow("show")
							messageForm.message.focus()
							updateInterval=setInterval("updateInfo()",3000);
							serverRes.innerHTML="Sign in"
							}
							}
							// Send message
							function sendMessage(){
								data="message="+messageForm.message.value+"&user="+signInForm.userName.value
								serverRes.innerHTML="Sending"
								Ajax_Send("POST","send.php",data,sentOk)
								}
				// Sent Ok
				function sentOk(res){
					if(res=="sentok"){
						messageForm.message.value=""
						messageForm.message.focus()
						serverRes.innerHTML="Sent"}
						else{
							serverRes.innerHTML="Not sent"
							}
							}
							lastReceived=0
							// Update info
							function updateInfo(){
								serverRes.innerHTML="Updating"
								Ajax_Send("POST","users.php","",showUsers)
								Ajax_Send("POST","receive.php","lastreceived="+lastReceived,showMessages)
								}
								// update online users
								function showUsers(res){
									usersOnLine.innerHTML=res
									}
				// Update messages view
				function showMessages(res){
					serverRes.innerHTML=""
					msgTmArr=res.split("<SRVTM>")
					lastReceived=msgTmArr[1]
					messages=document.createElement("span")
					messages.innerHTML=msgTmArr[0]
					chatBox.appendChild(messages)
					chatBox.scrollTop=chatBox.scrollHeight
					
					// Sign out
					if (signInForm.signInButt.name=="signOut"){
						data="user=" + signInForm.userName.value +"&oper=signout"
						Ajax_Send("POST","users.php",data,checkSignOut);
						return false;}
						
						
						// Sign out response
						function checkSignOut(res){
							if(res=="usernotfound"){
								serverRes.innerHTML="Sign out error";
								res="signout"
								}
								if(res=="signout"){
									hideShow("hide")
									signInForm.userName.focus()
									clearInterval(updateInterval)
									serverRes.innerHTML="Sign out"
									return false
									}
									}



here is the code for users.php
<?PHP
$olu=join("<br>",$onlineusers_file);
echo $olu;
function saveUsers($onlineusers_file){
$file_save=fopen("onlineusers.txt","w+");
flock($file_save,LOCK_EX);
for($line=0;$line<count($onlineusers_file);$line++){
fputs($file_save,$onlineusers_file[$line]."\n");
};
flock($file_save,LOCK_UN);
fclose($file_save);
}
$onlineusers_file=file("onlineusers.txt",FILE_IGNORE_NEW_LINES);
if (isset($_POST['user'],$_POST['oper'])){
$user=$_POST['user'];
$oper=$_POST['oper'];
$userexist=in_array($user,$onlineusers_file);
if ($userexist)$userindex=array_search($user,$onlineusers_file);
if($oper=="signin" && $userexist==false){
$onlineusers_file[]=$user;
saveUsers($onlineusers_file);
echo "signin";
exit();
}if($oper=="signin" && $userexist==true){
echo "userexist";
exit();
}
}
if($oper=="signout" && $userexist==true){
array_splice($onlineusers_file,$userindex,1);
saveUsers($onlineusers_file);
echo "signout";exit();
}
if($oper=="signout" && $userexist==false){
echo "usernotfound";
exit();
}
?>



Here is the code forsend.php
<?php
$message=strip_tags($_POST['message']);
$message=stripslashes($message);
$user=$_POST['user'];
$room_file=file("room1.txt",FILE_IGNORE_NEW_LINES);
$room_file[]=time()."<!@!>".$user.": ".$message;
	if (count($room_file)>20)$room_file=array_slice($room_file,1);
	$file_save=fopen("room1.txt","w+");
	flock($file_save,LOCK_EX);
	for($line=0;$line<count($room_file);$line++){
	fputs($file_save,$room_file[$line]."\n");
	};
	flock($file_save,LOCK_UN);
	fclose($file_save);
	echo "sentok";
	exit();
?>



here is the code for receive.php
<?php
$lastreceived=$_POST['lastreceived'];
$room_file=file("room1.txt",FILE_IGNORE_NEW_LINES);
for($line=0;$line<count($room_file);$line++){
$messageArr=split("<!@!>",$room_file[$line]);
if($messageArr[0]>$lastreceived)echo $messageArr[1]."<br>";
}
echo "<SRVTM>".$messageArr[0];
?>



If anyone can help it would be much appreciated.........

Is This A Good Question/Topic? 0
  • +

Replies To: Chat room is not working need help

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Chat room is not working need help

Posted 02 July 2009 - 01:12 PM

There are a few problems with your code (as you have already found out).

First is here:
Ajax_Send("POST","my_url","name1=ahmad&name2=511",reponseFunc);

At the end you have responseFunc, which Javascript is expecting to be a function, object, or variable (and is not - hence the undefined error). If you wanted to you could change that out with something like so:
function(){
  // whatever you want to have done on response from the Ajax
}

If you don't like that option you could define responseFunc, like so:
function responseFunc(){
  // whatever you want to have done on response from the Ajax
}


Now, another problem I see is that you don't seem to have Ajax_Send() defined in the code provided. Not sure if you have it somewhere else or what.

Your second error I am pretty sure is caused by this function:
function signInOut(){
				// Validationif (signInForm.userName.value=="" || signInForm.userName.value.indexOf(" ")>-1)
				{alert("Not valid user name\nPlease make sure your user name didn't contains a space\nOr it's not empty.");
				signInForm.userName.focus();
				return false;
				}



Specifically this line:
				// Validationif (signInForm.userName.value=="" || signInForm.userName.value.indexOf(" ")>-1)



You commented out an if statement, but not the opening bracket (and you have also taken out the closing bracket). Try fixing it like so:
function signInOut(){
				// Validation
				if (signInForm.userName.value=="" || signInForm.userName.value.indexOf(" ")>-1)
				{
						alert("Not valid user name\nPlease make sure your user name didn't contains a space\nOr it's not empty.");
						signInForm.userName.focus();
						return false; // doesn't allow the username
				}
				return true; // allows the username if no space is found in it
}



I have to go at the moment, but I'll check back about the last error later. Also, it is possible that the last error will be fixed by fixing the other errors.

Hope that helps so far.

<edit>
Okay, I looked through the chatbox.js and there are a few more problems with it. I have gone through and re-indented the whole file (some tabs were used) to make it more readable. Now, it has some issues with functions not ending, and items not being in functions. Here is the updated code:

function hideShow(hs){
    if(hs=="hide"){
        signInForm.signInButt.value="Sign in"
        signInForm.signInButt.name="signIn"
        messageForm.style.display="none"
        signInForm.userName.style.display="block"
        signInName.innerHTML=""
    }
    if(hs=="show"){
        signInForm.signInButt.value="Sign out"
        signInForm.signInButt.name="signOut"
        messageForm.style.display="block"
        signInForm.userName.style.display="none"
        signInName.innerHTML=signInForm.userName.value 
    }
}
// Sign in and Out
function signInOut(){
    // Validation
	if (signInForm.userName.value=="" || signInForm.userName.value.indexOf(" ")>-1){
	    alert("Not valid user name\nPlease make sure your user name didn't contains a space\nOr it's not empty.");
        signInForm.userName.focus();
        return false;
    }
    // Sign in
    if (signInForm.signInButt.name=="signIn"){
        data="user=" + signInForm.userName.value +"&oper=signin"
        Ajax_Send("POST","users.php",data,checkSignIn);
        return false;
    }
}
// Sign in response
function checkSignIn(res){
    if(res=="userexist"){
	    alert("The user name you typed is already exist\nPlease try another one");
        return false;
    }
    if(res=="signin"){
        hideShow("show")
        messageForm.message.focus()
        updateInterval=setInterval("updateInfo()",3000);
        serverRes.innerHTML="Sign in"
    }
}
// Send message
function sendMessage(){
    data="message="+messageForm.message.value+"&user="+signInForm.userName.value
    serverRes.innerHTML="Sending"
    Ajax_Send("POST","send.php",data,sentOk)
}
// Sent Ok
function sentOk(res){
    if(res=="sentok"){
        messageForm.message.value=""
        messageForm.message.focus()
        serverRes.innerHTML="Sent"
	}
    else{
        serverRes.innerHTML="Not sent"
    }
}
lastReceived=0
// Update info
function updateInfo(){
    serverRes.innerHTML="Updating"
    Ajax_Send("POST","users.php","",showUsers)
    Ajax_Send("POST","receive.php","lastreceived="+lastReceived,showMessages)
}
// update online users
function showUsers(res){
    usersOnLine.innerHTML=res
}
// Update messages view
function showMessages(res){
    serverRes.innerHTML=""
    msgTmArr=res.split("<SRVTM>")
    lastReceived=msgTmArr[1]
    messages=document.createElement("span")
    messages.innerHTML=msgTmArr[0]
    chatBox.appendChild(messages)
    chatBox.scrollTop=chatBox.scrollHeight
} // ADDED THIS TO END THE FUNCTION
// Sign out
/*if (signInForm.signInButt.name=="signOut"){ // EDITED OUT BECAUSE IT IS SUPPOSED TO BE IN A FUNCTION AND ISN'T
    data="user=" + signInForm.userName.value +"&oper=signout"
    Ajax_Send("POST","users.php",data,checkSignOut);
    return false;
}*/
// Sign out response
function checkSignOut(res){
    if(res=="usernotfound"){
        serverRes.innerHTML="Sign out error";
        res="signout"
    }
    if(res=="signout"){
        hideShow("hide")
        signInForm.userName.focus()
        clearInterval(updateInterval)
        serverRes.innerHTML="Sign out"
        return false
    }
}

</edit>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1