Salve utenti,
vi posto il codice di una semplicissima chat in php ajax che utilizza un file html al posto di un database mysql (per esempio) per la memorizzazione dei messaggi:
Codice PHP:
session_start(); ?>
<div id="chatbox">
<?php if(file_exists("log.html") && filesize("log.html") > 0){
$handle = fopen("log.html", "r");
$contents = fread($handle, filesize("log.html"));
fclose($handle);
echo $contents; } ?>
</div>
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" autocomplete="off" / class="a_11_b_n">
<input name="submitmsg" type="submit" id="submitmsg" value="Invia" class="a_11_b_n" /> </form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
//Load the file containing the chat log
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 1000); //Reload file every 2.5 seconds
});
</script>
Il codice della pagina post.php è semplicemente
Codice PHP:
session_start();
if(isset($_SESSION['nome_U'])){
$text = $_POST['text'];
$fp = fopen("log.html", 'a');
fwrite($fp, "(".date("H:i").") [b]".$_SESSION['nome_U']."[/b]: ".stripslashes(htmlspecialchars($text))."
");
fclose($fp);
}
Ora: la chat funziona benissimo con Chrome ma con internet explorer niente da fare.
Qualcuno mi spiega come mai per favore?