1
我想使用AJAX將多個SQL查詢加載到HTML表格中。我一直在使用教程來使用AJAX將值加載到選項框中作爲我的基礎,但無法使其工作。使用AJAX將SQL表格加載到HTML表格
<script>
$(document).ready(function($) {
var list_target_id = "tableDiv";
var list_select_id = "year";
var initial_target_html = "Fill in option boxes first";
$('#'+list_target_id).html(initial_target_html);
$('#'+list_select_id).change(function(e) {
var year = $(this).val();
var month = $(month).val();
var companyID = $(company).val();
if (year == "" && companyID == "" && month == "") {
$('#'+list_target_id).html(initial_target_html);
} else {
$.ajax({url: 'ajax-getvalues2.php?month='+month+'&year='+year&companyID='+companyID,
success: function(output) {
$('#'+list_target_id).html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " "+ thrownError);
}});
}
});
});
</script>
Ajax的getvalues2.php:
<?php
$connection = mysqli_connect("localhost", "Query", "Query", "database1");
$companyID = mysqli_real_escape_string($connection, $_GET['companyID']);
$month = mysqli_real_escape_string($connection, $_GET['month']);
$year = mysqli_real_escape_string($connection, $_GET['year']);
$date = $year + "/" + $month + "/01";
$date1 = $date.setMonth(date.setMonth()-1);
$date2 = $date.setMonth(date.setMonth()-2);
$date3 = $date.setMonth(date.setMonth()-3);
$date4 = $date.setMonth(date.setMonth()-4);
$date5 = $date.setMonth(date.setMonth()-5);
$date6 = $date.setMonth(date.setMonth()-6);
echo '<table id="myTable" class="table table-hover table-bordered">';
mysqli_select_db($connection, "database1");
$array = mysqli_query($connection, "SELECT DISTINCT buildingList.siteName, buildingList.siteId FROM buildingList INNER JOIN meterList ON buildingList.siteId = meterList.siteId WHERE buildingList.companyID = '$companyID' AND meterList.meterType = '$meterType'");
for (i=0, i < array.length, i++){
$siteId = $array[i];
$q2= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date1'");
$q3= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date2'");
$q4= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date3'");
$q5= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date4'");
$q6= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date5'");
$q7= mysqli_query($connection, "SELECT value FROM meterList WHERE siteId = '$siteId' AND meterType.meterList = '$meterType' AND date = '$date6'");
echo '<td>' array[i] "</td>";
echo '<td contenteditable='true' type="text" onkeyup="consumption()">" "</td>";
echo '<td>' .$row['q2']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q3']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q4']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q5']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q6']. '</td>';
echo '<td></td>';
echo '<td>' .$row['q7']. '</td>';
echo '<td></td>';
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($connection);
?>
月應輸入到PHP文件爲 「01」 的一月和今年應該是在HTML文件的底部
AJAX腳本2016年輸入爲「2016」。然後將其合併爲年/月/ 01等日期。
我不完全確定我哪裏出了問題,但我可以給我一些建議。在此先感謝:)
究竟是什麼工作?你看到一個錯誤?數據格式不正確?您必須向我們提供線索,以瞭解哪些功能無法正常工作。 –
@SloanThrasher我嘗試了一下瀏覽器並轉至http://localhost/ajax-getvalues2.php?month = 04&year = 2016&companyID = 1,但頁面只是空白,我會認爲那裏顯示的東西 – TakeoGFX
啓用錯誤reporing –