So I have an SQL database and table, and a php file calls the 10 results from it like this.
<?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);
?>
This is an JQuery ajax script I made but it doesn't seem to work
$(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;
What would be the best way to display this table from the php file on a separate index.html file. AJAX, JQuery AJAX, and XML are all options but I don't know how I would do this.
I ideally would want to display the result from the PHP file with document.getElementById('leaderboard').innerHTML because it would take all the html code, including the table.
Any help is greatly appreciated.