views:

49

answers:

0

Hi : )

when I Implemented chatting Function ,I used java Script to Send Data Between File To another . but I Faced some problem with it .

Code :

lastReceived=0;
var xmlhttp;
// Sign in and Out
function signInOut(){

// Sign in
if (signInForm.signInButt.name=="signIn"){
data="user=" + signInForm.userName.value +"&oper=signin"

Requst ("POST","users.php",data,checkSignIn)
return false
}
}
// Sign out

function singout(){

data="user=" + "" +"&oper=signout"
Requst ("POST","users.php",data,checkSignOut)

return false;     
}




// Sign in response
function checkSignIn(res){
if(res=="userexist"){
checkSignOut("signout");
checkSignIn("signin");
return false;
}



if(res=="signin"){

messageForm.message.focus()

updateInterval=setInterval("updateInfo()",3000);
serverRes.innerHTML="Sign in"
}
}

// Sign out response
function checkSignOut(res){
if(res=="usernotfound"){
serverRes.innerHTML="Sign out error";
res="signout"
}
if(res=="signout"){
 clearInterval(updateInterval)
serverRes.innerHTML="Sign out"
return false
}
}

// Update info
function updateInfo(){
serverRes.innerHTML="Updating"
Requst ("POST","users.php","",showUsers)
Requst ("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
}

// Send message
function sendMessage(){
data="message="+messageForm.message.value+"&user="+signInForm.userName.value
serverRes.innerHTML="Sending"
Requst ("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"
}
}
function  Requst (Action,URL,Data,Fun)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4)
eval(Fun(xmlhttp.responseText));
}
Data=encodeURI(Data);
xmlhttp.open(Action,URL,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length",Data.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(Data);


}

code of Send.php

<?php
session_start();
$user_id=$_SESSION['user_id'];
$data=$_POST['data'];
$message=strip_tags($_POST['message']);
$message=stripslashes($message);

include 'connent.php';
$MembernameR = mysql_query("select MemberName from members where MemberID = '$user_id'");
      $Membername = mysql_fetch_row($MembernameR);
     $user = $Membername[0];
$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();
?>

I think the problem is in Function "Requst" it does not Work or the session in File Send.php

can you tell me How I can Fix it ? or can you Tell me why the above code it doesn't work well ? :(