School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,416 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,578 people online right now. Registration is fast and FREE... Join Now!




Chat room is not working need help

 

Chat room is not working need help, Chat room is not working need help

colorodo13

2 Jul, 2009 - 11:11 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2009
Posts: 24

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
CODE

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

Ajax_Send("POST","my_url","name1=ahmad&name2=511",reponseFunc);


Here is the code for chatbox.js
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(){
                // 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
CODE

<?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
CODE

<?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
CODE

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

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: Chat Room Is Not Working Need Help

2 Jul, 2009 - 12:12 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
There are a few problems with your code (as you have already found out).

First is here:
CODE
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:
CODE
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:
CODE
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:
CODE
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:
CODE
                // 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:
CODE
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:

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(){
// 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>
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:30PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month