0

我有一个名为 reg.php 的表单,它的操作是 reg.php,我希望单击提交时选择的下拉值保持选中状态,到目前为止我所做的如下所示

我检查了发布的问题,但无法将其与我的代码匹配

<?php
if( $_POST['registerbtn']){
$selected_value = $_POST['selectID'];
$query = mysql_query("SELECT  linecard_name FROM selection WHERE select_id = '$selected_value'");
$rows=mysql_fetch_assoc($query); 
$linecard_name= $rows['linecard_name'];
$sql = "SELECT select_id, linecard_name FROM selection " . "ORDER BY linecard_name";
$rs = mysql_query($sql);

while($rownw = mysql_fetch_array($rs)){
if(  $rownw['linecard_name'] == $linecard_name)  {
$options = "<option  selected =selected  value=".$rownw['select_id']."> " .$rownw['linecard_name']. " </option> ";
}
}
}

require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
while($rownw = mysql_fetch_array($rs)){
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
} 
mysql_close() ;

$form = "<form action='./reg.php'  method='post'> 
<table>
<tr>
<td>  </td>
<td>  <font color='red'> $errormsg </font> </td> 
</tr>

<tr>
<td> Select  Linecard </td> 
<td> <Select name='selectID' >  <option value = '0'>  Select  from here </option> $options  </select></td> 
</tr>

<tr> 
<td  > <input type='submit' name='registerbtn' value='Register' />   </td> 
</tr> 
</table> 
echo $form;

单击注册按钮之前的选择语句用于填充数据库中的下拉列表,我只想在用户完成特定选择并单击注册按钮时保持选中的项目,在这种情况下,我的代码填充下拉列表再次但现在用户选择的项目被选中但没有清除下拉列表中先前填写的项目,即在单击注册按钮之前,请帮助

4

1 回答 1

1

我发现用变量来做是最干净的,像这样:

while($rownw = mysql_fetch_array($rs)){
  $selected = '';
  if ($rownw['select_id'] == $selected_value){
    $selected = 'selected="selected"';
  }
  $options .= "<option value = ".$rownw['select_id']." $selected > ".$rownw['linecard_name']. " </option> ";
}
于 2012-06-15T05:09:30.043 回答