我想用我从this问题改编的脚本来显示表单。该脚本位于我编写的名为queries.js的文件中,其目的是在位于我的项目索引中的div这样的div中打印名为“dbMinAlert.php”的php表单的内容,我尝试在我的索引中调用getNewData();
.php文件使用这个标签<body onLoad="getNewData()">
,但它似乎没有做任何事情。如何在特定时间检索带有AJAX的PHP表单
var data_array = ''; // this is a global variable
function getNewData() {
$.ajax({
url: "dbMinAlert.php",
})
.done(function(res) {
data_array = res; // the global variable is updated here and accessible elsewhere
getNewDataSuccess();
})
.fail(function() {
// handle errors here
})
.always(function() {
// we've completed the call and updated the global variable, so set a timeout to make the call again
setTimeout(getNewData, 2000);
});
}
function getNewDataSuccess() {
//console.log(data_array);
document.getElementById("recentExits").innerHTML=data_array;
}
getNewData();`
---这个PHP代码的工作原理,它实际上做我期望它做的事情。真正的问题在于JavaScript,对于我所关心的下一个PHP表单可能会打印出“Hello world”消息,但我希望它显示在放置在索引中的div内,而不必将任何内容发布到dbMinAlert.php。
define("HOST", "localhost");
define("DBUSER", "root");
define("PASS", "password");
define("DB", "mydb");
// Database Error - User Message
define("DB_MSG_ERROR", 'Could not connect!<br />Please contact the site\'s administrator.');
$conn = mysql_connect(HOST, DBUSER, PASS) or die(DB_MSG_ERROR);
$db = mysql_select_db(DB) or die(DB_MSG_ERROR);
$query = mysql_query("
SELECT *
FROM outputs, products
WHERE products.idProduct=outputs.idProduct
ORDER BY Date DESC, Time DESC limit 5
");
echo '<ul class="news">';
while ($data = mysql_fetch_array($query)) {
$date = date_create($data['Date']);
$time = date_create($data['Time']);
echo '<li><figure><strong>'.date_format($date,'d').'</strong>'.date_format($date,'M').date_format($date,'Y').'</figure>'.$data["idProduct"]." ".$data['prodName'].'</li>';
}
echo '</ul>';
切勿使用mysql_ *,它PHP7被删除。 – Drudge
你什么时候调用'getNewData'? – Rayon
我在我的索引'body onLoad中调用它,但它仍然不起作用 – All