-1

我有一个名为 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> 
<td  > <input type='submit' name='registerbtn' value='Register' />   </td> 
</tr> 
</table> 
4

2 回答 2

0

提交表单后获取发布值并与下拉值进行比较,即

<?php
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
$options = "<option value= '0' >  Select  from here </option>";
 $_POST['prefix']=isset($_POST['prefix']) ? $_POST['prefix'] : '';
    while($rownw = mysql_fetch_array($rs)){
    $sel='';
        if($rownw['select_id']==$_POST['prefix']){
          $sel="selected='selected'";
        }
     $options .= "<option value = '".$rownw['select_id']."' ".$sel." > ".$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>  Username  </td>
<td>  <input type='text' name='user' />  </td> 
</tr> 

<tr>
<td> Select  Linecard </td> 
<td> <Select name='selectID'> $options </select>

尝试这个

于 2012-05-11T16:32:15.770 回答
0

如果您确保表单操作与表单位于同一页面,您将能够访问 POSTed 数据。

于 2012-05-11T16:26:54.547 回答