validation - 在 CodeIgniter 中重新填充选择表单字段

标签 validation forms select codeigniter

我想在验证后重新填充(即重新填充)从选择表单字段发布的值。我知道 codeigniter 用户指南建议使用 set_select() 函数,但指南中的示例假设您已经对“select”表单元素的值数组进行了硬编码(在 HTML 中)。就我而言,我使用了表单助手,并且我的选择字段值是从 array() 中提取的,如下所示:


$allstates = array('blah','blah2','blah3','blah4','blah5');
echo form_label('Select State Affiliate', 'state'); 
echo form_dropdown('state', $allstates);

不用说,$allstates 数组是动态的并且会随着时间的推移而变化。那么我该如何编码,以便可以用用户选择的值重新填充我的选择字段呢?

最佳答案

您可以通过 form_dropdown 的第三个参数设置应预选的值。用户在上一步中选择的值位于 $this->input->post('state') 中。所以你会使用:

echo form_dropdown('state', $allstates, $this->input->post('state'));

来自用户指南:

The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected

$options = array(
              'small'  => 'Small Shirt',
              'med'    => 'Medium Shirt',
              'large'   => 'Large Shirt',
              'xlarge' => 'Extra Large Shirt',
            );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

关于validation - 在 CodeIgniter 中重新填充选择表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3209238/

相关文章:

JavaScript 表单验证仍然在 'False' 上提交表单

sql-server - T-SQL COALESCE GROUPING 将 SETS 组合成没有 NULL 重复项的单列

mysql - 比较sql表中的哈希值

php - php 和 select mysql 之间的比较问题

ruby-on-rails - Rails 模型验证条件 allow_nil?

javascript - 将类附加到元素angularjs后,ng-class可以设置超时吗?

c++ - 在 C++ 中验证 double

Django:如何在表单标签中包含 url 链接

excel - 如何确保在 InputBox 中输入的数字小于在 VBA 中 InputBox 中输入的另一个数字?

regex - Flex 3 正则表达式问题