我想访问用户选择的值,以便进行进一步处理。
因此,我使用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的地址?
我认为你的意思是当你改变下拉菜单,值不保留,它显然不会因为你的页面正在刷新,你需要从url GET
的值,并把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;
?>