我单击一个HTML按钮来运行一个JS脚本,该脚本将日期条件(在页面的其他位置输入)发布到PHP页面,然后PHP页面查询MySQLi数据库。我想在自动生成的HTML表中显示结果,我希望该表位于页面上的特定div中。我的HTML代码是:
<div id="buttonContainer">
<input type="button" id="reportButton" value="Generate Report" onclick="generateReport();">
</div>
<div id="displayTable">
</div>
我的JS代码是:
window.generateReport = function(){
$.post("PhotoTopTen.php", {
startdate: startDate,
enddate: endDate,
});
}
我的PHP代码是:
<?php
$startdate = date('Y-m-d H:i:s',strtotime($_POST["startdate"]));
$enddate = date('Y-m-d H:i:s',strtotime($_POST["enddate"]));
$con = mysqli_connect("localhost","User","password","dbase");
$sql="SELECT Document, COUNT(*) as count FROM table WHERE event LIKE 'Photo%' GROUP BY Document ORDER BY count DESC LIMIT 10";
$result = mysqli_query($con,$sql);
$data = array();
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
};
$colNames = array_keys(reset($data))
echo "<table><tr>";
foreach($colNames as $colName) {
echo "<th>$colName</th>";
}
echo "</tr>";
foreach($data as $row) {
echo "<tr>";
foreach($colNames as $colName) {
echo "<td>".$row[$colName]."</td>";
}
echo "</tr>";
}
echo "</table>";
&GT;
我知道MySQLi查询有效,因为我在phpMyAdmin中测试了它。我也知道我的错误是将HTML标签回显到JS文件(而不是HMTL页面),但我只是不知道如何以其他方式做到这一点。我尝试在JS脚本标签中回显HTML,但这似乎也不起作用。
答案 0 :(得分:0)
你错过了在div中显示结果。请尝试以下方法:
$.post("PhotoTopTen.php", {startdate: startDate,enddate: endDate}, function( data ) {
$( "#displayTable" ).html( data );
});