第二个下拉列表没有显示任何房间

时间:2012-01-28 13:28:30

标签: php jquery

我有两个下拉列表。第一个下拉列表显示数据库中的建筑物列表。当用户从下拉菜单中选择建筑物时,则在第二个下拉菜单中,它应显示与在第一个下拉菜单中选择的建筑物相关联的房间列表。问题是它在选择建筑物后没有显示房间列表,它只显示值“请选择”而没有别的。

如何在用户从第一个下拉菜单中选择建筑物后,在下拉菜单中显示房间列表?

下面是建筑物和房间字段来自的SQL表(表的名称是Room):

Building                Room

Canalside West          CW4/10
Canalside West          CW5/10
Canalside East          CE1/04
Canalside East          CE2/02

PHP代码:

 $sql = "SELECT DISTINCT Building FROM Room"; 

 $sqlresult = mysql_query($sql); 

 $buildings = array(); // easier if you don't use generic names for data 

 $buildingHTML = "";  
 $buildingHTML = "<form action=\"\" method=\"post\">"; 
 $buildingHTML .= '<select name="buildings" id="buildingssDrop" onchange="getRooms();">'.PHP_EOL; 
 $buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

 while($sqlrow = mysql_fetch_array($sqlresult)) 
 { 
     $building = $sqlrow['Building']; 
     $buildingHTML .= "<option value='".$building."'>" . $building . "</option>".PHP_EOL;  
  } 

  $buildingHTML .= '</select>'; 
  $buildingHTML .= '</form>'; 

  $roomHTML = "";  
  $roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL; 
  $roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
  $roomHTML .= '</select>'; 

  echo $buildingHTML;  
  echo $roomHTML; 

为动态人口添加脚本。

JS CODE:

function getRooms() { 
    var building = jQuery("#buildingssDrop").val(); 
    jQuery('#roomsDrop').empty(); 
    jQuery('#roomsDrop').html('<option value="">Please Select</option>'); 
    jQuery.ajax({ 
          type: "post", 
          url:  "rooms.php", 
          data: "building="+building, 
          success: function(response){ 
              jQuery('#roomsDrop').append(response); 
          } 
        }); 
 } 

这里的rooms.php是您需要为动态数据填充调用的ajax文件:

rooms.php

$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 .= "<option value='".$room."'>" . $room . "</option>".PHP_EOL;  
} 


echo $roomHTML; 

当我做一个警报(响应)以测试响应显示的内容时,它会显示DOCTYPE,',,'标签,但它在标签之间没有显示任何内容。我希望通往rooms.php的途径是正确的。 create_session.php和rooms.php位于同一文件夹中。两者的网址都在这里:

create_session.php:http://helios.hud.ac.uk/u777587/Mobile_app/create_session.php rooms.php:http://helios.hud.ac.uk/u777587/Mobile_app/rooms.php

在create_session.php中只需在文本框中键入info101并按提交然后在底部附近您将看到建筑物和房间的两个下拉菜单

0 个答案:

没有答案