2014-03-25 45 views
0

我想訪問用戶選擇的值以供進一步處理。當我更改產品時,下拉菜單不顯示更改

因此,我使用post方法來傳遞整個表單的值。 但GET訪問cust_id,以便我可以反映我的表單的更多部分 中的更改。因此,我不得不張貼以下行:

<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'> 

外部php代碼。但現在,當我從下拉式菜單中的某些選項,URL相應的變化,但是下拉菜單沒有反映變化

<?php 
    $query = "SELECT Cust_id, Cust_Name, Cust_City FROM Customers"; 
    $result = mysql_query($query); 
?> 

<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'> 

<?php 
    while($row = mysql_fetch_assoc($result)) { 
     echo '<option value="'.$row['Cust_id'].'">'.$row['Cust_Name'].','.$row['Cust_City'].'</option>'; 
    } 
    echo '</select>'; 
?> 

我怎樣才能在相同的形式,從訪問數據庫中的特定客戶ID的地址當用戶從這個下拉菜單中選擇客戶名稱?

+1

請注意,因爲它們是不安全的,過時不應該使用mysql_ *函數了。 http://www.php.net/manual/de/migration55.deprecated.php。改用PDO或mysqli。 –

回答

1

我認爲你的意思是當你改變下拉列表時,這個值不會被保留,顯然不會因爲你的頁面被刷新,你需要GET來自url的值,並且把一個selected屬性設置爲選中該值。

這樣來做:

<?php 
$query = "SELECT Cust_id,Cust_Name,Cust_City FROM Customers" ; 
$result = mysql_query($query); 
//checking if GET variable is set, if yes, get the value 
$selected_option = (isset($_GET['product'])) ? $_GET['product'] : ''; 
//we will store all the dropdown html code in a variable and display it later 
$select = "<select id='fullname' onChange=\"window.location='sp_menu.php?product='+this.value\" name='fullname'>"; 
while($row = mysql_fetch_assoc($result)) { 
//checking if the cust_id matches the GET value, 
//if yes, add a selected attribute 
$selected = ($selected_option==$row['Cust_id'])?'selected':''; 
echo '<option value="'.$row['Cust_id'].'"'. $selected. '>' . $row['Cust_Name'] .' , '.$row['Cust_City']. '</option>'; 
} 
$select .= '</select>'; 
//display the dropdown 
echo $select; 
?>