所以我有一個SQL數據庫和表格,並且一個php文件會像這樣調用它的10個結果。使用ajax或jquery顯示來自php的ajax表格
<?php
$servername = getenv('IP');
$username = getenv('C9_USER');
$password = "";
$database = "c9";
$dbport = 3306;
$con = mysqli_connect($servername, $username, $password, $database, $dbport);
if (!$con) {
die("Error! Check your internet connection and try again!");
}
mysqli_select_db($con, "users");
$query = "SELECT * FROM users LIMIT 10";
$result = mysqli_query($con, $query);
echo "<table>
<tr>
<th>ID</th>
<th>Village</th>
<th>Power</th>
<th>Influence</th>
<th>Economy</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['village'] . "</td>";
echo "<td>" . $row['power'] . "</td>";
echo "<td>" . $row['influence'] . "</td>";
echo "<td>" . $row['economy'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
這是一個JQuery的AJAX腳本,我做了,但它似乎沒有工作
$(document).ready(function(){
var xmlhttp = new XMLHttpRequest();
if(xmlhttp==null){
alert("Your browser does not support AJAX!");
return false;
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//document.getElementById("divTable").innerHTML=xmlhttp.responseText;
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "/server.php", true);
xmlhttp.send(null);
});
return false;
什麼是顯示在一個單獨的index.html從PHP文件此表的最佳方式文件。 AJAX,JQuery的AJAX和XML都是選項,但我不知道我會如何做到這一點。
我理想的情況是想顯示PHP文件的結果與document.getElementById('leaderboard').innerHTML
,因爲它會採取所有的HTML代碼,包括表。
任何幫助,非常感謝。
使用需要附加jQuery的文件,這個Ajax代碼。 –