-5
我創建了proba.html文件getuser.php,數據庫名爲'mobilni',表名爲'imena'。代碼有什麼問題,它沒有顯示錶的結果,只有空白?Php和Ajax未顯示SQL的結果
proba.html代碼:
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
<form>
<select name="okrug" onchange="showUser(this.value)">
<option value="">Okrug:</option>
<option value="Raski">Raski</option>
<option value="Banatski">Banatski</option>
<option value="Backi">Backi</option>
<option value="Beogradski">Beogradski</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
getuser.php代碼:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','mobilni');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"mobilni");
$sql="SELECT * FROM imena WHERE Okrug = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Ime</th>
<th>Okrug</th>
<th>Telefon</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Okrug'] . "</td>";
echo "<td>" . $row['Telefon'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
請做一些調試工作 - 讀取日誌,var_dump vars等。沒有人會爲你做。 –
我嘗試過,但日誌中沒有任何錯誤。 – Mike
你測試了你的查詢嗎?它甚至會返回什麼嗎?嘗試調試,你會很容易地縮小它的範圍。 – Neat