我有一个网站,我需要放一个聊天框但是当有人写的时候,用户必须刷新整个页面来阅读文本,但我需要让它自动更新数据请帮助我。
现在这是代码:
<?
session_start();
include("includes/config.php");
if($_GET['with']){
if($_SESSION['login']){
if($_SESSION['login'] == $_GET['with']){
header("Location: index.php");
}else{
$id = $_SESSION['login'];
$with = intval($_GET['with']);
if($_POST['submit']){
$text = $_POST['text'];
if(empty($text)){
}else{
$query = mysqli_query($connect,"INSERT INTO chat(`from`,`to`,`topic`) VALUES('$id','$with','$text')");
}
}
?>
<form method="post" action="chat.php?with=<?=$with?>">
<textarea name="text" placeholder="Write Here..." style="text-align:right;resize:none;width:100%;height:200px;font-size:24">
</textarea>
<br/>
<input type="submit" name="submit" value="Send"/>
</form>
<div id="chat">
<?
$query = mysqli_query($connect,"SELECT * FROM users WHERE id='$id'");
$f = mysqli_fetch_array($query);
$query = mysqli_query($connect,"SELECT * FROM users WHERE id='$with'");
$ff = mysqli_fetch_array($query);
$query = mysqli_query($connect,"SELECT * FROM chat order by id desc");
while($fetch = mysqli_fetch_array($query)){
if($fetch['from'] == $with && $fetch['to'] == $id or $fetch['from'] == $id && $fetch['to'] == $with){
if($fetch['from'] == $f['id']){
echo "<div style='word-wrap: break-word;'>".$f['fname']." ".$f['lname'].":<br/>".$fetch['topic']."</div>";
}
if($fetch['from'] == $ff['id']){
echo "<div style='max-width:200px;word-wrap: break-word;'>".$ff['fname']." ".$ff['lname'].":<br/>".$fetch['topic']."</div>";
}
}
}?>
</div>
<?}
}else{
header("Location: index.php");
}
}else{
header("Location: index.php");
}
&GT;
答案 0 :(得分:0)
您必须使用AJAX并通过javascript
仅更新网站的一部分(聊天窗口)在这里查看如何操作: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_ajax_async
答案 1 :(得分:0)
你可以使用ajax来做到这一点。你可以查看这个教程, Chat box using ajax
答案 2 :(得分:0)
只使用php就无法达到你想要的效果。但是,您可以使用ajax来完成您的要求。有一些提供源代码的例子。以下是其中一些:
答案 3 :(得分:0)
Ajax 会对您有所帮助。但是在同一个代码中混合html javascript和php的方式是一种不好的做法;像这样,很多人很难帮助你用ajax调整你的代码。