在 CodeIgniter 中重新填充选择表单字段
我想在验证后重新填充(即重新填写)从选择表单字段发布的值。我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过 form_dropdown 的第三个参数设置应预选的值。用户在上一步中选择的值位于 $this->input->post('state') 中。所以你会使用:
从用户指南:
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:
From the user guide:
有时,特别是在处理对象时,如果没有表单助手,可能会更容易做到这一点:
我知道它非常丑陋,但在很多情况下,这是使用对象时最简单的方法对象而不是关联数组。
Sometimes, especially when working with objects, it might be easier to do this without the form helper:
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.