2016-02-26 176 views
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> 

任何人都可以幫助我我想從數據庫中顯示所選建築物的地板。如果我選擇建築物,地板將顯示在選擇樓層。當選擇選項更改時從數據庫顯示選擇選項

+0

樓層信息存儲在哪裏?數據庫結構也會有幫助。 – mitkosoft

+0

它在建築物數據庫中。 – jayson

+0

無疑他們在那裏。表格,結構,任何東西... – mitkosoft

回答

1

不知道您的數據庫設置或結構這裏的是你如何能做到這一點的例子:

<?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> 

您需要張貼的參數傳遞到在SQL的WHERE子句。請注意,您應該從發佈的參數中轉義惡意代碼。

我也假設你有一個form標籤這裏面的html代碼與一起提交按鈕

+0

只是好奇這種方法 - 你將執行第二PHP部分代碼(樓層相關),而無需刷新頁面後選擇上面的建築? – mitkosoft

+0

@mitkosoft他不問如何在不刷新頁面的情況下實現這一點。 – karlingen

0

您應該使用AJAX來獲取數據庫值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");} 
} 
}); 
}); 

和PHP顯示FUNCTION應該是

$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>'; 
    } 
+0

如果沒有提交,是否運行此代碼? – jayson

+0

當您選擇