Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing AJAX to return query results based on drop down box
    primarykey
    data
    text
    <p>I know that this is a popular question and I have looked at many examples trying to get my head around <code>AJAX</code> and <code>jQuery</code>.</p> <p>I have a simple situation with one drop down box when changed sends an <code>AJAX</code> request for the results of an <code>SQL</code> query based on the drop down box selection. </p> <p>The page loads correctly and the function is being called when a department is being chosen from the drop down box (the alert tells me that) but I am not getting any return data. In trying to identify the problem how can I tell whether the getTeachers.php file is actually running?</p> <p><strong>Web page</strong> Script for calling getTeacher.php on the server</p> <pre><code>&lt;script src="http://localhost/jquery/jquery.min.js"&gt; &lt;/script&gt; &lt;script&gt; function checkTeacherList(str) { var xmlhttp; if (str=="") { document.getElementById("txtTeacher").innerHTML=""; return; } 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 &amp;&amp; xmlhttp.status==200) { document.getElementById("txtTeacher").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getTeachers.php?q="+str,true); xmlhttp.send(); alert(str); //To test it is getting this far, which it does } &lt;/script&gt; </code></pre> <p>Drop down box and txtTeacher ID for return data from server</p> <pre><code>&lt;select name="department_list" id="department_list" onchange="checkTeacherList(this.value);" &gt; &lt;?php $options[0] = 'All'; $intloop = 1; while($row = mysql_fetch_array($department_result)) { $options[$intloop] = $row['departmentName']; $intloop = $intloop + 1; } foreach($options as $value =&gt; $caption) { echo "&lt;option value=\"$caption\"&gt;$caption&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;div id="txtTeachers"&gt;Teacher names&lt;/div&gt; </code></pre> <p><strong>Server side PHP - getTeachers.php</strong></p> <pre><code>&lt;?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error($con)); } $db_selected = mysql_select_db("iobserve"); $sql="SELECT * FROM departments WHERE departmentName = '".$q."';"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo $row['teacherName']; } mysql_close($con); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload