我有一个 HTML 网站,其中有一个页面使用 PHP 代码来查询 MySQL 数据库。该表包含 3 列(日期、时间、剩余)。根据当前日期和时间,我希望 HTML 页面返回并显示“剩余”列中的相应值。
诀窍是我希望这个值自动更新(AJAX?)而无需用户刷新页面或单击按钮。
到目前为止,我使用的 PHP 的工作原理是它根据日期显示“剩余”值,但我还没有弄清楚如何查询日期和时间,也不知道如何自动刷新。
到目前为止我已经弄清楚的 PHP 代码
(注意:quantitytest_config.php
只包含主机名/用户名/密码)
<?php
include 'quantitytest_config.php';
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("grace59_countdown",$dbhandle)
or die("Could not select countdown");
//execute the SQL query and return records
$result = mysql_query("SELECT remaining FROM quantity WHERE salesdate=CURDATE()");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "Quantity:".$row{'remaining'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>
在此先感谢您的帮助。非常感谢。
最佳答案
与 jQuery.load
, 轻而易举,以下代码片段将调用您的 PHP 脚本并每 10 秒更新一次剩余计数。
setInterval(function(){
// Assumes the container has the id of count
// and the php script is called remaining.php
$("#count").load("remaining.php")
}, 10000);
如果没有 jQuery,您将不得不阅读一些内容,但这并不难
至于您在同一问题中嵌入的任何其他问题。您不应将多个问题作为一个问题提出。每个问题都应显示您尝试过的内容、预期/实际行为、错误消息。这就是您的问题对其他人有用的方式,而不仅仅是您自己,而这正是 Stack Overflow 的核心所在。目前你的问题是我不知道该怎么做,这不太适合 SO。
关于php - 更新 "quantity remaining"而不刷新站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048657/