Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had to do this recently myself. Here's what I did, feel free to use what makes sense to you. I tried what seemed like a simple way to myself. </p> <pre><code>&lt;select name="department_list" id="department_list" onchange="$('#singleUser').load('yourextraphppage.php?nid='+this.value);"&gt; &lt;option value='none'&gt;Select a department&lt;/option&gt; </code></pre> <p>Then load your first select list with data and close the select tag... Add a blank div where you want the 2nd select. I used the value from the first select list for my parameter for the 2nd select list's query. (it was the foreign key in the 2nd table for the 2nd select list)</p> <pre><code>&lt;div id="singleUser" class="singleUser"&gt; &lt;/div&gt; </code></pre> <p>Last, you need the additional PHP page that you called from the first select list. Here's a barebones version of my code.</p> <pre><code>echo "&lt;option value='none'&gt;Select user&lt;/option&gt;"; try { $db = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); $stmt = $db-&gt;prepare("SELECT dm.empid AS empid, u.name AS name FROM mytable dm JOIN mytable2 u ON dm.empid = u.id WHERE dm.deptid = :id ORDER BY u.name"); $stmt-&gt;bindParam(':id', $id); $stmt-&gt;execute(); while ($r = $stmt-&gt;fetch()) { $empid = $r['empid']; $userName = $r['name']; echo "&lt;option value='".$empid."'&gt;".$userName."&lt;/option&gt;"; } echo "&lt;/select&gt;"; echo "&lt;/p&gt;"; $db = null; } catch (PDOException $ex) { echo "An Error occurred!"; } </code></pre> <p>I hope this helps. I'm rather busy so I can't go in detail explaining it at the moment. I'll check back later to see if you have any questions.</p>
 

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