2
<tr>
  <td><label class="label label-success">Building Name: </label></td>
  <td>
    <select class="form-control" name="building_name" required>
      <option value="">Select Building</option>
      <?php
      include '../Database/db.php';
      $res=mysql_query("SELECT * FROM building");
      while($row = mysql_fetch_array($res)){
      ?>
      <option value="<?php echo $row['building_name']; ?>"><?php echo $row['building_name']; ?></option>

      <?php } ?>
    </select>
  </td>
</tr>
<tr>
  <td><label class="label label-success">Floor: </label></td>
  <td>
    <select class="form-control" name="floor" required>
      <option value="">Select Floor</option>
      <option>></option>

    </select>
  </td>
</tr>

can anyone help me i want to display the floor of the selected building from the database. if i choose the building the floor will be display on select option floor.

karlingen
  • 13,800
  • 5
  • 43
  • 74
jayson
  • 31
  • 5

2 Answers2

1

Without knowing your database setup or structure here's is an example of how you could achieve this:

<?php require_once('../Database/db.php'); ?>
<tr>
  <td><label class="label label-success">Building Name: </label></td>
  <td>
    <select class="form-control" name="building_name" required">
      <option value="">Select Building</option>
      <?php
      $res=mysql_query("SELECT * FROM building");
      while($row = mysql_fetch_array($res)){
      ?>
      <option value="<?php echo $row['building_name']; ?>"><?php echo $row['building_name']; ?></option>
      <?php } ?>
    </select>
  </td>
</tr>
<tr>
  <td><label class="label label-success">Floor: </label></td>
  <td>
    <select class="form-control" name="floor" required>
      <option value="">Select Floor</option>
      <?php
      $building_name = mysql_real_escape_string($_POST['building_name']);
      $res2=mysql_query("SELECT * FROM floor WHERE building_id = '{$building_name}'");
      while($row = mysql_fetch_array($res2)){
      ?>
      <option value="<?php echo $row['floor_name']; ?>"><?php echo $row['floor_name']; ?></option>
      <?php } ?>
    </select>
  </td>
</tr>

You need to pass the posted parameter to an WHERE clause in the SQL. Note that you should be escaping malicious code form the posted parameters.

I'm also assuming that you have this html code inside a form tag along with a submit button

karlingen
  • 13,800
  • 5
  • 43
  • 74
  • just curious with this approach - how you will execute second PHP part of code (floor related) without refreshing the page after selecting the buiding above? – mitkosoft Feb 26 '16 at 10:09
  • @mitkosoft He is not asking how to achieve this without refreshing the page. – karlingen Feb 26 '16 at 10:10
0

You should use AJAX to get the database value to display in DROPDOWN

$('#building_name').change(function(){
var building_name=  $('#building_name').val();
$.ajax({
url: "index.php", //Change it yours
type: "post",
data: {building_name: building_name}, 
success: function(data) {
    if(data != "false")
{
    $('#Floor').html(data); 
}
else{
alert("No value");}
}
});
});

And PHP FUNCTION should be

  $building_name = mysql_real_escape_string($_POST['building_name']);
  $res2=mysql_query("SELECT * FROM building  WHERE building_name = '{$building_name}'");
  while($row = mysql_fetch_array($res2)){
  echo '<option value="'.$row['floor_name'].'">'.$row['floor_name'].'</option>';
  } 
Gopalakrishnan
  • 957
  • 8
  • 19