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

时间:2018-01-25 06:18:54

标签: php codeigniter

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

我以前的代码是:在申请之前选择

<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>

应用set set后:但语法错误

<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>

2 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码来帮助您

<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>

答案 1 :(得分:0)

我删除语法错误

<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>