Forum Moderators: open

Message Too Old, No Replies

ajax not defined? trying to make a chat system

         

wildheart25c

6:11 pm on Mar 29, 2011 (gmt 0)

10+ Year Member



Good day. I'm trying to make a small chat prg for me and my friend. not a public chat

chatbox.php
this is the page where me and my friend access. there's a small text field and a send button. one second passes and the history is loaded. when i enter my msg and press send, the message is saved in the chattable


<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
die('Could not connect: ' . mysql_error());

$db_found = mysql_select_db("chatdb", $con);

if ($db_found)
{
$query = "insert into chattable (username, message, msgread) values ('".$_POST['username']."','".$_POST['message']."','0')";
$result = mysql_query($query);
}
?>

<head>
<script type="text/javascript" src="javascript/prototype.js"></script>
<script language = "javascript">
var int=self.setInterval("insertTime()",2000);
function insertTime()
{
var ajaxx = new Ajax.Updater('datetime', 'loadHistory.php', {
method: 'get',
insertion: Insertion.Bottom
});
}
</script>
</head>

<body>
<form name="form1" id="form1" method="post">
<input name="sendmessage" type="submit" value="send">
</form>
<br />

<div id="datetime">

</div>

</body>
</html>



loadhistory.php
this is the page that ajax accesses. basically, every second it checks for new messages and posts it in the chatbox page. so when i send a message, it should appear after a second or two in chatbox.php. when the new message is displayed, the msgread filed is set to 1 so that when ajax accesses loadhistory, it wont print the entire history again, just the new messages

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
die('Could not connect: ' . mysql_error());

$db_found = mysql_select_db("chatdb", $con);

if ($db_found)
{
$query = "select username, message from chattable where msgread = '0'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{
while ($rows = mysql_fetch_array($result))
{
echo "<table> \n";
echo "<tr> \n";
echo "<td>".$rows['username']."</td>";
echo "<td>".$rows['message']."</td>";
echo "<tr> \n";
echo "</table> \n";
}
}

$query = "update chathistory set msgread = 1 where msgread = '0'";
$result = mysql_query($query);
}
?>



when i load chatbox.php i get error. the yellow triangle at the very bottom of the ie page. i double clicked on it and saw the errors. ajax not identified.
here's how my files are saved:
temp folder has chatbox and loadhistory php files AND a folder called javascript. inside this folder there's the prototype.php file.
thts it.

chattable has 4 fields
id which is the primary key int length 1
username varchar
message varchar
msgread int


so why isnt this working? something tells me that even if i get the ajax error fixed, that it still wont work like a chat system.

wildheart25c

6:12 pm on Mar 29, 2011 (gmt 0)

10+ Year Member



correction. prototype.js file. not php file. typo sorry. there's no edit button

Clarkie

6:13 pm on Mar 29, 2011 (gmt 0)

10+ Year Member



I can't help you as I'm new to all this but I thought I'd point out that there should be an edit button to the left of your post underneath "user profile."

caribguy

7:20 pm on Mar 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var ajaxx = new Ajax.Updater('datetime', 'loadHistory.php', {

is 'ajaxx' a typo?

wildheart25c

7:43 pm on Mar 29, 2011 (gmt 0)

10+ Year Member



no afraid not. i renamed it to ajax and aja#*$! and i got an error.
i tried running it in FF and it worked. i checked my table and it would fill up every second with an empty row. i hope you all understand what im trying to do. i might be going about it the wrong way. i dont no.