vi posto il jquery e il php sopratutto il php
la funzione ajax:
<script type="text/javascript">
$(document).ready(function(){
timestamp = 0;
updateMsg();
$("form#chatform").submit(function(){
$.post("backend.php",{
mio_id: $("#valore_mio_id").val(),
message: $("#msg").val(),
name: $("#author").val(),
action: "postmsg",
time: timestamp
}, function(xml) {
$("#msg").empty();
addMessages(xml);
});
return false;
});
});
function addMessages(xml) {
if($("status",xml).text() == "2") return;
timestamp = $("time",xml).text();
$("message",xml).each(function(id) {
message = $("message",xml).get(id);
$("#messagewindow").prepend(""+$("author",message).text()+
": "+$("text",message).text()+
"
");
});
}
function updateMsg() {
$.post("backend.php",{ time: timestamp }, function(xml) {
$("#loading").remove();
addMessages(xml);
});
setTimeout('updateMsg()', 1000);
}
</script>
ed il php sul quale devo modificare(penso)
<?php
// Configuration
$dbhost = "localhost";
$dbuser = "****";
$dbpass = "***";
$dbname = "***";
$store_num = 10;
$display_num = 10;
// Script
error_reporting(E_ALL);
header("Content-type: text/xml");
header("Cache-Control: no-cache");
$dbconn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$dbconn);
foreach($_POST as $key => $value)
{
$$key = mysql_real_escape_string($value, $dbconn);
}
if(@$action == "postmsg")
{
mysql_query("INSERT INTO messages (`user`,`msg`,`time`,`from`)
VALUES ('$name','$message',".time().",'$mio_id')",$dbconn );
mysql_query("DELETE FROM messages WHERE id <= ".
(mysql_insert_id($dbconn)-$store_num),$dbconn);
}
$messages = mysql_query("SELECT user,msg
FROM messages
WHERE time>$time
ORDER BY id ASC
LIMIT $display_num",$dbconn);
if(mysql_num_rows($messages) == 0) $status_code = 2;
else $status_code = 1;
echo "<?xml version=\"1.0\"?>\n";
echo "<response>\n";
echo "\t<status>$status_code</status>\n";
echo "\t<time>".time()."</time>\n";
if($status_code == 1)
{
while($message = mysql_fetch_array($messages))
{
$message['msg'] = htmlspecialchars(stripslashes($message['msg']));
echo "\t<message>\n";
echo "\t\t<author>$message[user]</author>\n";
echo "\t\t<text>$message[msg]</text>\n";
echo "\t</message>\n";
}
}
echo "</response>";
?>