///this is the form part .where i am trying get data from database and show it in "txt" part.the script part is in same file and getService.php is in the same directory as this file.why showing nothing when i select something.
<form>
<select name="parent">
<option selected="users" onchange="showService(this.value)">Select a Service:</option>
<?php
$res=$mysqli->query("SELECT * FROM service");
while($row=$res->fetch_array())
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
</br>
</form>
<div id="txt"><b>Service info will be show here...</b></div>
</center>
</div>
//script part. in same file.
<script>
function showService(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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txt").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getService.php?q="+str,true);
xmlhttp.send();
}
}
</script>
//getService.php
<?php
session_start();
include_once'db_connect.php';
?>
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$sql="SELECT * FROM service WHERE id = '".$q."'";
$result = mysqli_query($mysqli,$sql);
echo "<table>
<tr>
<th>id</th>
<th>Service name</th>
<th>Detail</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($mysqli);
?>
</body>
</html>