当我更改产品时,下拉菜单不显示更改
问题描述:
我想访问用户选择的值以供进一步处理。当我更改产品时,下拉菜单不显示更改
因此,我使用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的地址当用户从这个下拉菜单中选择客户名称?
答
我认为你的意思是当你改变下拉列表时,这个值不会被保留,显然不会因为你的页面被刷新,你需要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;
?>
请注意,因为它们是不安全的,过时不应该使用mysql_ *函数了。 http://www.php.net/manual/de/migration55.deprecated.php。改用PDO或mysqli。 –