Help making live chat
Help making live chat
I cant manage to get the div to auto-update correctly with the latest posts.
index.php
-
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
<html>
<head><title>Live Chat</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function update()
{
$.post("update.php", {}, function(data){ $("div.chat_box").val(data);});
setTimeout('update()', 1000);
}
$(document).ready(function()
{
update();
$("#say_msg").click(function()
{$.post("update.php",
{action: "add_msg", msg: $("#message").val()},
function(data){
$("div.chat_box").val(data);
$("#message").val("");
}
);
}
);
});
</script>
</head>
<body>
<div class="chat_box"><center>Chat Is Loading</center></div>
<input id="message" size="40">
<input type="submit" id="say_msg" value="Say">
</body>
</html>
update.php
-
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
$connect = mysql_connect("sql305.000space.com", "space_3514259", "namir1");
if(!$connect) {
die('Can\t connect to host : ' . mysql_Error());
}
$db_selected = mysql_select_db('space_3514259_ForsakenRealms', $connect);
if (!$db_selected) {
die ('Can\'t connect to DB : ' . mysql_error());
}
if(isset($_POST['action']) && $_POST['action'] != "")
switch ($_POST['action'])
{
case add_msg: add_message($_POST['msg']); break;
case del_msg: del_message($_POST['id']); break;
case load_msg: load_messages(); break;
}
function add_message($message)
{
$poster = "user1";
$sql = "insert into chat(message, poster) values ('$message', '$poster')";
$query = mysql_query($sql) or die("Error adding new message.");
}
function del_message($id){}
function load_messages()
{
$sql2 = "SELECT * FROM chat ORDER BY `id` DESC";
$query2 = mysql_query($sql2) or die("error 2 ".mysql_error());
$return = "";
while($row = mysql_fetch_assoc($query2))
{
$return = $row['poster'].">".$row['message']."<br />".$return;
}
echo $return;
}
load_messages();
?>
Once I manage to get the basics out of the way I shouldnt need help with the rest of it.