2013-01-20 219 views
1

我想創建一個2下拉列表。當第一個下拉列表被選中時,第二個下拉列表會更改值。我似乎無法獲得第二個下拉列表中插入的名稱。這是我的代碼下拉選項更改時選擇

<script type='text/javascript'> 
$(window).load(function(){ 
$("#category").change(function() { 
    if ($(this).data('options') === undefined) { 
     /*Taking an array of all options-2 and kind of embedding it on the select1*/ 
     $(this).data('options', $('#select2 option').clone()); 
    } 
    var id = $(this).val(); 
    var options = $(this).data('options').filter('[value=' + id + ']'); 
    $('#select2').html(options); 
    //alert(options); 
}); 
}); 

</script> 

<form action = '' method = 'POST'> 
    <select name="select1" id="category"> 
     <option><--Destinations--></option> 
     <?php 
      $destination = mysql_query("SELECT * FROM destination"); 
      while ($row = mysql_fetch_assoc($destination)) 
      { 
       $destid = $row['destination_id']; 
       $destname = $row['destination_name']; 
       echo"<option value=".$destname.">".$destname."</option>"; 
      } 

     ?> 
</select> 

<select name="items" id="select2"> 
    <option><--Hotels--</option> 
    <?php 
     $hotel = mysql_query("SELECT * FROM hotel"); 
     while ($row = mysql_fetch_array($hotel)) 
     { 
      $hotel_location = $row['hotel_location']; 
      $hotel_name = $row['hotel_name']; 
      echo"<option value=".$hotel_location.">".$hotel_name."</option>"; 
     } 
    ?> 
</select> 
<input type = "submit" name="add"> 
</form> 



<?php 
if(isset($_POST['select1'],$_POST['items'])) 
{ 
    $destination_name = $_POST['select1']; 
    $hotel_name = $_POST['items']; 
    echo $destination_name; 
    //echo $destination_id; 

} 
?> 

它已經輸出的酒店位置,但我似乎無法找到一種方式來獲得選定的酒店名稱。請幫助

+0

我得到一個錯誤上線21未定義sqlsquery當我在網上運行 –

+0

一些代碼從數據庫中來,這樣的錯誤會一直顯示。 – arukiri123

+0

當我在一個單獨的字符串中定義它時,它就會消失,然後將它作爲執行傳遞給mysqlquery –

回答

0

嘗試

<script type='text/javascript'> 
    $(window).load(function(){ 
    $("#category").change(function() { 
     if ($(this).data('options') === undefined) { 
     /*Taking an array of all options-2 and kind of embedding it on the select1*/ 
     $(this).data('options', $('#select2 option').clone()); 
    } 
    var id = $(this).val(); 
    var options = $(this).data('options').filter('[value=' + id + ']'); 
    $('#select2').html(options); 
    //alert(options); 
    }); 
    }); 

    </script> 

    <form action = '' method = 'POST'> 
    <select name="select1" id="category"> 
    <option><--Destinations--></option> 
    <?php 
     $sql = "SELECT * FROM destination"; 
     $destination = = $conn->Execute($sql); 
     while ($row = $destination) 
     { 
      $destid = $row['destination_id']; 
      $destname = $row['destination_name']; 
      echo"<option value=".$destname.">".$destname."</option>"; 
     } 

    ?> 
    </select> 

<select name="items" id="select2"> 
<option><--Hotels--</option> 
<?php 
    $sql = "SELECT * FROM hotel"; 
    $hotel = $conn->Execute($sql); 
    while ($row = $hotel) 
    { 
     $hotel_location = $row['hotel_location']; 
     $hotel_name = $row['hotel_name']; 
     echo"<option value=".$hotel_location.">".$hotel_name."</option>"; 
    } 
?> 

+0

沒有更改 – arukiri123

+0

如何獲取$ hotel_name的值? – arukiri123

+0

是目的地成功嗎?同樣地爲酒店申報sql並執行sql來獲取酒店名稱請參閱上面編輯的代碼 –