Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto populate dropdown based on previous selection
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3912302/how-to-dynamically-populate-options-on-dropdown-lists-based-on-selection-in-anoth">How to dynamically populate options on dropdown lists based on selection in another dropdown?</a> </p> </blockquote> <p>I have MySQL tables looking like that</p> <p>regions table</p> <pre><code>id | region ------------------- 1 | Region1 2 | Region2 </code></pre> <p>...</p> <p>and schools table</p> <pre><code>id | school ------------------- 1 | schno1 1 | schno5 1 | schno6 2 | scho120 </code></pre> <p>There are multiple select (dropdown) menus in my registration form. The regions dropdown looks like that</p> <pre><code>&lt;td&gt; &lt;select name="region" id="region"&gt; &lt;option value=""&gt;Region&lt;/option&gt; &lt;?php $result=$db-&gt;query("SELECT * FROM regions"); while($row=$result-&gt;fetch_array(MYSQLI_BOTH)) { echo '&lt;option value="'.$row[0].'"&gt;'.$row[1].'&lt;/option&gt;'; } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt; &lt;div id="sch_dropdown"&gt; &lt;select name="school" id="school"&gt; &lt;option value=""&gt;School&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;span id="loading"&gt;&lt;img src="core/design/img/jquery-ui/loading.gif" width="16" height="16" align="absmiddle"&gt;&amp;nbsp;Loading...&lt;/span&gt; &lt;div id="no_sch"&gt;No schools found&lt;/div&gt; &lt;/td&gt; </code></pre> <p>I want to populate new dropdown list based on previous selections. What i want to do is, get "regions" id, then populate schools dropdown menu based on id (id of previous selection) from "schools" table on the fly. Here is my code but it doesn't work.</p> <p>My js looks like that</p> <pre><code>$(document).ready(function(){ function populate() { fetch.doPost('core/code/includes/search.php'); } $('#region').change(populate); var fetch = function() { var schools = $('#schools'); return { doPost: function(src) { $('#loading').show(); $('#sch_dropdown').hide(); $('#no_sch').hide(); if (src) $.post(src, { region_code: $('#region').val() }, this.getschools); else throw new Error('No SRC was passed to getCounties!'); }, getschools: function(results) { if (!results) return; schools.html(results); $('#loading').hide(); // Hide the Loading... $('#sch_dropdown').show(); // Show the drop down } } }(); populate(); }); </code></pre> <p>and search.php</p> <pre><code>if(isSet($_POST['region_code'])) { $res=$db-&gt;query("SELECT * FROM schools WHERE id='".$_POST['region_code']."'"); while ($row = $res-&gt;fetch_array(MYSQLI_BOTH)) { echo '&lt;option value="'.$row[0].'"&gt;'.$row[1].'&lt;/option&gt;'; } } </code></pre> <p>Please help me to finish it. Thx in advance. </p>
    singulars
    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.
    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