I wish to get result of semester in dropdown list from db, while selecting course from previous dropdown list. I'm begginer in php. Below is the code I tried.
AJAX CODE:
<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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").value=xmlhttp.responseText;
}
};
xmlhttp.open("GET","getuser1.php?q="+str,true);
xmlhttp.send();
}
}
</script>
HTML:
<tr class="light">
<th height="43">COURSE</th>
<td><select name="course" onChange="showUser(this.value)">
<option value="">Select a course</option>
<option value="UG">UG</option>
<option value="PG">PG</option>
</select></td>
</tr>
<tr class="dark">
<th height="43">SEMESTER</th>
<td>
<select name="sem" id="txtHint">
</select>
</td>
GETUSER1.php:
<?php
$q =$_GET['q'];
mysql_connect("localhost","root","");
mysql_select_db("internal");
$sql="SELECT * FROM tblcourse WHERE course = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "<option>".$row['sem']."</option>";
}
?>