Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to change your PHP code to acheive this scenario. If you want the rooms to be dynamical populated when changing the building name you can user jquery. Include jQUery library in your script.You need to do an ajax call to dynamically populate the list.</p> <p>I have added only from building part. Rest all above is same. In the foreach loop remove the building from the array.Change your current php code to this.</p> <p>PHP CODE:</p> <pre><code> $sql = "SELECT DISTINCT Building FROM Room"; $sqlresult = mysql_query($sql); $buildings = array(); // easier if you don't use generic names for data $buildingHTML = ""; $buildingHTML = "&lt;form action=\"\" method=\"post\"&gt;"; $buildingHTML .= '&lt;select name="buildings" id="buildingssDrop" onchange="getRooms();"&gt;'.PHP_EOL; $buildingHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; while($sqlrow = mysql_fetch_array($sqlresult)) { $building = $sqlrow['Building']; $buildingHTML .= "&lt;option value='".$building."'&gt;" . $building . "&lt;/option&gt;".PHP_EOL; } $buildingHTML .= '&lt;/select&gt;'; $buildingHTML .= '&lt;/form&gt;'; $roomHTML = ""; $roomHTML .= '&lt;select name="rooms" id="roomsDrop"&gt;'.PHP_EOL; $roomHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; $roomHTML .= '&lt;/select&gt;'; echo $buildingHTML; echo $roomHTML; </code></pre> <p>You need to add a script for dynamic population.</p> <p>JS CODE:</p> <pre><code>function getRooms() { var building = jQuery("#buildingssDrop").val(); jQuery('#roomsDrop').empty(); jQuery('#roomsDrop').html('&lt;option value=""&gt;Please Select&lt;/option&gt;'); jQuery.ajax({ type: "post", url: "rooms.php", data: "building="+building, success: function(response){ jQuery('#roomsDrop').append(response); } }); } </code></pre> <p>Here the rooms.php is the ajax file you need to call for dynamic population of data:</p> <p>rooms.php</p> <pre><code>$building = isset($_POST['building']) ? $_POST['building'] : ''; $sql = "SELECT Room FROM Room WHERE Building ='".$building."'"; $sqlresult = mysql_query($sql); $roomHTML = ""; while($sqlrow = mysql_fetch_array($sqlresult)) { $room = $sqlrow['Room']; $roomHTML .= "&lt;option value='".$room."'&gt;" . $room . "&lt;/option&gt;".PHP_EOL; } echo $roomHTML; </code></pre> <p>Hope this helps for you</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