簡體   English   中英

現場實時更改

[英]Live changes on site

我試圖讓一個PHP文件中,我會從數據庫中發送數據,並通過他們,我會顯示0或1。在我的例子,我有兩個user_ids一張桌子和一個參數in其取值0或1。從測試.php我通過提供user_id填寫表格,然后提交。 當我為此用戶提交參數時in如果參數為1,則參數變為0,反之亦然。 next.php中,我使用<iframe src="show.php"> ,在show.php中,我顯示user_idin 我想要的是當我提交user_id時,立即查看show.php中的更改。 我所做的就是一直刷新頁面,但是太麻煩了。 你能提出其他建議嗎? 這是一些代碼。

test.php的

<?php
require_once 'include_php/db.php';
global $dbcnx;
?>
<form action="" method="get">
    <input type="text" name="id"/>
    <input type="submit" name="submit"/>
</form>

<?php

if(isset($_GET['submit']))
{
    $id = $_GET['id'];
    $res = mysqli_query($dbcnx, "select * from people where uid = ".$id.";");
    $row = mysqli_fetch_array($res);

    if($row['in'] == 0) $up = mysqli_query($dbcnx, "UPDATE `people` SET `in`=1 WHERE uid=".$id.";");
    else $up = mysqli_query($dbcnx, "UPDATE `people` SET `in`=0 WHERE uid=".$id.";");
}

show.php

<?php
require_once 'include_php/db.php';
global $dbcnx;

$res = mysqli_query($dbcnx, "select * from people");

while($row = mysqli_fetch_array($res))
{
    echo $row['uid']." ".$row['in']."<br/>";
}

print "<script>window.location.replace('show.php');</script>"; //here is the disturbing refresh

next.php

<iframe src="show.php">

您想要的是實時更新,而無需硬刷新頁面。 您可以使用AJAX請求或websockets來實現

使用ajax請求,您可以輕松地設置超時功能,每隔x秒/分鍾刷新一次iframe。

這個問題很好地說明了如何進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM