Help - Search - Members - Calendar
Full Version: What Is Wrong?
Zymic Webmaster Forums > Web Design & Development > Client Side Scripting > JavaScript
TehBlizzy
CODE
<html>
<head>
<title> Chatroom </title>
</head>
&lt;script language="Javascript">
var xmlHttp

function sendMessage()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var Msg=document.getElementById("UIMessage");
var url="ChatFunc.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
var Data = "UserMsg=" + encodeURI(document.getElementById("address").value);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", data.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(Data);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("ChatT").innerHTML+=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
</script>
<body>
<style type="text/css">
body
{
background-color: #000000;
}
div#Main
{
position: block;
width: 600px;
height: 500px;
margin: auto;
border-style: solid;
border-color: #FF0000;
border-width: 1px;
}
div#Chat
{
position: relative;
width: 395px;
height: 345px;
border-style: solid;
border-color: #FF0000;
border-width: 1px;
left: 5px;
top: 5px;
color: #FF0000;
}
p#ChatT
{
position: relative;
overflow-y: scroll;
height: 285px;
left: 5px;
width: 390px;
}
div#ChatH
{
color: #FF0000;
font-size: 25px;
text-align: center;
border-bottom-style: solid;
border-bottom-width: 1px;
font-family: Comic Sans MS;
}
div#UsersH
{
color: #FF0000;
font-size: 25px;
text-align: center;
border-bottom-style: solid;
border-bottom-width: 1px;
font-family: Comic Sans MS;
}
div#Users
{
position: absolute;
width: 180px;
height: 345px;
border-style: solid;
border-color: #FF0000;
border-width: 1px;
left: 750px;
top: 15px;
}
p#UsersT
{
position: relative;
overflow-y: scroll;
height: 285px;
left: 5px;
width: 175px;
color: #FF0000;
font-size: 20px;
}
div#Input
{
position: relative;
width: 585px;
height: 135px;
border-style: solid;
border-color: #FF0000;
border-width: 1px;
top: 10px;
left: 5px;
color: #FF0000;
font-size: 24px;
font-family: Comic Sans MS;
}
div#Input p
{
position: relative;
margin: auto;
}
</style>
<div id="Main">
<div id="Chat">
<div id="ChatH">
CHATROOM
</div>
<p id="ChatT">
lol
</p>
</div>
<div id="Users">
<div id="UsersH">
USERS
</div>
<p id="UsersT">
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
lol
<br>
</p>
</div>
<div id="Input">
<p>
Message:<input type="text" id="UIMessage" size="64" maxlength="64">
<button onClick="sendMessage()">SEND</button>
</p>
</div>
</div>
</body>
</html>


Please ignore my horrible layout because I a too lazy to put my CSS in another file xD

Basically, if I take out the "requestHeader" lines the page actually does something, but when I add it the script does nothing.
I know this because when I click Send the ChatT box becomes empty. But, my PHP script is not receiving the UserMsg value I am sending to it when it actually works.

ChatFunc.php
CODE
<?php
$UserMsg=$_POST['UserMsg'];
$File=fopen("ChatT.txt", "a");
fwrite($File,$UserMsg);
fclose($File);
?>


If I set $UserMsg to a certain value, like "Hello", it writes it to the file. If I assign it to the post data it writes a "0" which Im sure means null or something.

Any tips? biggrin.gif

The site is http://tehblizzy.uuuq.com/ and the text file is (tehblizzy.uuuq.com/ChatT.txt)
(Excuse my bad language in it, I was very po'd at it at the time...)
Andrew
Did you name the file file.php ?
also your missing your doctype declaration.
TehBlizzy
CODE
$File=fopen("ChatT.txt", "a");



I have no clue and have never used a doctype for anything, so I don't know the purpose of one... and where it goes.

CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


I added that to my index.php page and it didn't change anything.
MrTouz
CODE
&lt;script language="Javascript">


At the very beginning of your code

i dunno if it has to do something with your error, or if it's how it is on your page but you should replace &lt; by <
swordz
Also, that <script></script> tag should be inside the<head></head> section.

swordz
TehBlizzy
I placed the script stuff inside the header and no changes, and the &lt; thing was just a mess up by the code thingy, its an actual < in my page
Andrew
Then it's not a messup, try deleting the one you see and replacing it. Might be surprised that it works.
TehBlizzy
Thats not the problem at all...Its a < even in notepad and in the "View Source" of the page...
MrTouz
Yea delete the < and replace it by a <. Sometimes it's just that.

Sometimes i have errors i can't understand and just by copying my whole doc into a new page and save makes it works. Dunno why it happens but characters sometimes gets messed up.
Andrew
Your code doesn't do anything, the JavaScript doesn't do anything either.
TehBlizzy
CODE
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", data.length);
xmlHttp.setRequestHeader("Connection", "close");

Take that out and it works, but the PHP script doesn't get the message data when you send it.

And that is my problem... everything else is fine.

-

The < isn't a problem at all so stop suggesting things about it. I already know that isn't a problem. ( I've uploaded the page MANY times and have only changed the requestHeader part of the AJAX script so I know it isn't a problem...)
Ed
I've thrown it through the excellent js debugger firebug: https://addons.mozilla.org/en-US/firefox/addon/1843

The first bug is that you use an element id that doesn't exist 'address', either rename the 'UIMessage' to 'address', otherwise change line 21:

CODE
var Data = "UserMsg=" + encodeURI(document.getElementById("address").value);


The second bug is you're using 'data' as the object name when it should be 'Data':

CODE
xmlHttp.setRequestHeader("Content-length", data.length);


Those were the ones I spotted hope that helps.
TehBlizzy
Wow, I'm so blind to have missed that... Thanks for the help laugh.gif
TehBlizzy
Woohoo!! Its basically done now! I will add a username thing later once I get the sessions going, but for now its nothing but "Someone: (ur msg)"
TehBlizzy
Alrighty, its basically done now. Only thing is that will freeze up if you spam it... happened once already. Another thing is, is that IE doesn't seem to like it according to a friend of mine who tried it.
TehBlizzy
Now there ish a new problem. Sessions aren't saving. I know I'm doing it right so its not my coding that has probs. The session thing may have been posted somewhere in these forums on how to fix it or why it is happening, but using the search tool comes up with too many results. SO yea, sessions don't work :'(
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.