如何动态下拉列表集选择使用



是这样做的。 这是我从数据库结果填充的动态下拉列表。 当我提交表单时,验证应用,如果空字段发生验证错误,表单将停止提交。但是所有下拉列表也会删除其值,所以我再次填写所有表单而不是空字段

我以前的代码是:在应用设置之前选择

<select name="position_filled_against_id" id="position_filled_against_id">
 <option value="">Select</option>
<?php
foreach($position_filled_against as $position_filled)
{
 $selected = "";
 echo '<option value="'.$position_filled->position_filled_against_id.'" >'.$position_filled->code.'-'.$position_filled->name.'</option>';
}
?>
</select>

应用后设置选择:但语法错误

<select name="position_filled_against_id" id="position_filled_against_id"><option value="">Select</option>
<?php
  foreach($position_filled_against as $position_filled)
  {
     $selected = "";
     echo '<option value="'.set_select("position_filled_against_id",$position_filled->position_filled_against_id,TRUE).'" >'.$position_filled->code.'-'.$position_filled->name.'</option>';
  }
  ?>
</select>

你可以试试下面的代码,它会帮助你

<select name="position_filled_against_id" id="position_filled_against_id">
<option value="">Select</option>
<?php
foreach($position_filled_against as $position_filled)
{
 $selected = "";
 if($_REQUEST['position_filled_against_id']==$position_filled->position_filled_against_id)
 {
   $selected = 'selected="selected"';
 }
 echo '<option value="'.$position_filled->position_filled_against_id.'" '.$selected.' >'.$position_filled->code.'-'.$position_filled->name.'</option>';
}
?>
</select>

我删除了语法错误

<select name="position_filled_against_id" id="position_filled_against_id">
<option value="">Select</option>
<?php
    foreach($position_filled_against as $position_filled)
    {
     $selected = "";
     echo '<option value="'.$position_filled->position_filled_against_id.'" '.set_select("position_filled_against_id","$position_filled->position_filled_against_id", TRUE).' >'.$position_filled->code.'-'.$position_filled->name.'</option>';
     }
?>
</select>

最新更新