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

发布于 2024-09-09 05:11:27 字数 434 浏览 3 评论 0原文

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


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

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

I want to re-populate (i.e refill) values posted from a select form field after validation. I know codeigniter user guide proposed using set_select() function but their example from the guide assume you've hard-coded (in HTML) the array of values for the 'select' form element. In my case I used the Form Helper and my select field values are pulled from an array() like so:


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

Needless to say, $allstates array is dynamic and changes over time. So how do I code such that my select field can be re-populated with the value the user selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-09-16 05:11:27

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

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

从用户指南:

第一个参数将包含字段的名称,第二个参数将包含选项的关联数组,第三个参数将包含您希望选择的值

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

You can set the value that should be preselected via the third paramater of form_dropdown. The value that the user selected in the previous step is in $this->input->post('state'). So you would use:

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

From the user guide:

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>
空名 2024-09-16 05:11:27

有时,特别是在处理对象时,如果没有表单助手,可能会更容易做到这一点:

<select name="my_select>
    <?php foreach ( $my_object_with_data as $row ): ?>
    <option value="" <?php if ( isset( $current_item->title ) AND $row->title == $current_item->title ) { echo 'selected="selected"';} ?> >
        <?php echo $row->title; ?>
    </option>
    <?php endforeach; ?>
</select>

我知道它非常丑陋,但在很多情况下,这是使用对象时最简单的方法对象而不是关联数组。

Sometimes, especially when working with objects, it might be easier to do this without the form helper:

<select name="my_select>
    <?php foreach ( $my_object_with_data as $row ): ?>
    <option value="" <?php if ( isset( $current_item->title ) AND $row->title == $current_item->title ) { echo 'selected="selected"';} ?> >
        <?php echo $row->title; ?>
    </option>
    <?php endforeach; ?>
</select>

I know it's very ugly, but in a lot of cases this was the easiest way of doing it when working with objects instead of an associative array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文