gpt4 book ai didi

javascript - 现场实时更改

转载 作者:行者123 更新时间:2023-11-30 16:43:18 24 4
gpt4 key购买 nike

我正在尝试制作一个 php 文件,我将从数据库发送数据并通过它们显示 0 或 1。在我的示例中,我有一个包含两个 user_id 和一个参数 in 的表。它取值 0 或 1。在 test.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。

This question很好地描述了如何试一试。

关于javascript - 现场实时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31612070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com