当我开始在文本字段中书写时,它没有被写入,它被更新并且网站开始严重滞后。该怎么办?
[聊天.php]
<?php> session_start();
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<script type="text/javascript">
// setTimeout("window.location.reload()",3000);
</script>
<body>
<script>
$(document).ready (function (
){ show();setInterval('show()',5000);
$("#button").bind("click", function (){
var message = $("#text").val();
$.ajax ({
url: "chat.php",
type: "POST",
data: {message},
dataType: "json"
});
});
});
function show()
{
$.ajax({
url: "chat.php",
cache: false,
success: function(html){
$("#content").html(html);
}
});
}
</script>
<div id="content">
<?php
include("config.php");
if(isset($_POST['message'])){
$sql = "insert into `messages` (`message`) values ('".$_SESSION['login'].": ".$_POST['message']."')";
mysql_query($sql);
}
$sql = "select message from `messages` ORDER BY id desc LIMIT 10";
$res = mysql_query($sql);
if($res)
{
while($row = mysql_fetch_object($res))
{
printf("<ul type='none'><li>%s</li></ul>",$row->message);
}
}
else
{
echo "<p> <b>Error: ".mysql_error()."</b> </p>";
exit();
}
?>
<input type='text' id="text" name='message'>
<button id="button">Saada</button>
</div>
</body>
</html>
像这样尝试