Jump to content

Ajax - Refresh Chat Not Working


Recommended Posts

Hello There!

 

I am trying to get my chat room, to automatically update the messages and data. As of right now, to see a new message, you must refresh the page. ( I learned from w3schools and don't quite get AJAX as much as I want to. :/ So if I'm way off track, please tell me. ) As always, here is the code I am using:

 

&--#60;?php
session_start();
if(!session_is_registered(myusername)){
header("location:/login/main_login.php");
}

$myusername = $_SESSION['myusername'];

?&--#62;




&--#60;html&--#62;
&--#60;head&--#62;
&--#60;link rel="SHORTCUT ICON" href="http://oi46.tinypic.com/rky444.jpg"&--#62;
&--#60;title&--#62;Mbox:Chat&--#60;/title&--#62;

&--#60;script type='text/javascript'&--#62;
function updateChat()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("topscreen").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET",/chat/chatroom.php?error=",true);
xmlhttp.send();
}
&--#60;/script&--#62;

&--#60;style&--#62;

/*3DS SCREEN START*/

div#topscreen
{
       border:15px solid;
       border-color:#1E90FF;
       overflow:auto;
position: absolute;
top: 0px;
left: 0px;
height: 205px;
width: 290px;
background-color:#FFFFFF;
z-index:2;

}

div#bottomscreen
{
position: absolute;
top: 218px;
left: 0px;
height: 212px;
width: 320px;
background-color:#1E90FF;

}

/*3DS SCREEN STOP*/

div#taskbar
{
background-color:silver;
width:320px;
height:100px;
position:absolute;
top:30px;
z-index:5;
}

&--#60;/style&--#62;
&--#60;meta name="viewport" content="width=320"&--#62;
&--#60;/head&--#62;
&--#60;body&--#62;

&--#60;body onload="setInterval('updateChat()',5000);"&--#62;

&--#60;!-- TOP SCREEN --&--#62;
&--#60;div id="topscreen"&--#62;

&--#60;?php

$db_sitename="spencer_register";
$db_hostname="localhost";
$db_username="spencer_mega";
$db_password="PASSWORD HERE";
$no_of_comments="15";

/* Leave the script below as it is */
mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_sitename);
$pagename=md5($_SERVER['PHP_SELF']);
$query=mysql_query("Select * from comments where comment_on='$pagename' ORDER BY id DESC LIMIT 0, $no_of_comments");


while($fetch=mysql_fetch_array($query)) {
echo "&--#60;p&--#62;&--#60;b&--#62;&--#60;sub&--#62;".$fetch['comment_by'].": &--#60;/sub&--#62;&--#60;/b&--#62;&--#60;sub&--#62;".$fetch['comment']."&--#60;/sub&--#62;&--#60;/p&--#62;";
}
mysql_close();
?&--#62;


&--#60;/div id="topscreen"&--#62;

&--#60;!-- BOTTOM SCREEN --&--#62;
&--#60;div id="bottomscreen"&--#62;

&--#60;!-- PUT STUFF ON THE TASKBAR UNDER THE DIV ID TASKBAR --&--#62;
&--#60;div id="taskbar"&--#62;

&--#60;a href="../homepage.php"&--#62;&--#60;img src="http://the12.squarespace.com/storage/zzzzhome.png?__SQUARESPACE_CACHEVERSION=1356027845932" width="50px" height="50px"&--#62;&--#60;/a&--#62;

&--#60;/div id="taskbar"&--#62;
&--#60;!-- Send input box start --&--#62;

&--#60;form action="/chat/post_chat.php" method="post"&--#62;
&--#60;table&--#62;
&--#60;tr&--#62;&--#60;td&--#62; &--#60;/td&--#62;&--#60;td&--#62;&--#60;input type="hidden" name="comment_on" size="" readonly="readonly"  value="&--#60;?php print md5($_SERVER['PHP_SELF']); ?&--#62;"/&--#62;&--#60;/td&--#62;&--#60;/tr&--#62;
&--#60;input type="hidden" name="comment_by"  value="&--#60;?php echo $myusername;?&--#62;"/&--#62;
&--#60;tr&--#62;&--#60;td&--#62;&--#60;input name="comment" size="33" maxlength="255" &--#62;&--#60;input type="submit" value="Send!" /&--#62;&--#60;/td&--#62;&--#60;/tr&--#62;
&--#60;br/&--#62;&--#60;br/&--#62;&--#60;br/&--#62;&--#60;br/&--#62;&--#60;br/&--#62;&--#60;br/&--#62;&--#60;br/&--#62;&--#60;br/&--#62;
&--#60;tr&--#62;&--#60;td&--#62;&--#60;/td&--#62;&--#60;td&--#62;&--#60;/td&--#62;&--#60;/tr&--#62;
&--#60;/table&--#62;
&--#60;/form&--#62;

&--#60;/div id="bottomscreen"&--#62;


&--#60;/body&--#62;
&--#60;/html&--#62;

 

Yeah I know. =P Some messy coding, but I'm working on it.

 

Alright, well thanks in advance! :)

 

~ Spencer

 

 

  • Like 1
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...