如何在表单验证失败后重新填充codeigniter中的下拉? [英] how to repopulating a dropdown in codeigniter after form validation fails?

查看:113
本文介绍了如何在表单验证失败后重新填充codeigniter中的下拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<select style="width:25%;" name='state' <?php if(form_error('state') != '') {  echo 'id=error'; } ?>>
<option value=''>State</option>
 <?php  foreach($states->result() as $row)
 {
 echo "<option value='$row->state_name'>". $row->state_name."</option>";
 }  ?>
</select>

hi,我面临上述代码中的问题,请帮助我,
set_value is不是为我工作,下拉不填充后表单验证失败,感谢提前

i am facing problem in the above code, please help me, set_value is not working for me, dropdown is not populating after form validation fails , thanks in advance or

这样的工作,这里的目标是隐藏的值。现在如何将目标值设置为下拉值(选项值)

does something like this works, here target is hidden value. now how to set target value to drop down value(option value)

  <script>
    window.onload = function (){
    target=document.getElementById('target').value;
        if(target.length!=''){
   // document.getElementByName('state').value=target;
        }
    }
    </script>


推荐答案

根据手册:

<select name="myselect">
        <option value="one" <?php echo  set_select('myselect', 'one', TRUE); ?> >One</option>
        <option value="two" <?php echo  set_select('myselect', 'two'); ?> >Two</option>
        <option value="three" <?php echo  set_select('myselect', 'three'); ?> >Three</option>
</select>



在您的情况下



In your case

<select style="width:25%;" name='state' <?php if(form_error('state') != '') {  echo 'id=error'; } ?>>
<option value=''>State</option>
 <?php  foreach($states->result() as $row)
 {
 echo "<option value='$row->state_name' " . set_select('state', $row->state_name) . " >". $row->state_name."</option>";
 }  ?>

</select>

[UPDATE]

<select style="width:25%;" name='state' <?php if(form_error('state') != '') {  echo 'id=error'; } ?>>
    <option value=''>State</option>
     <?php  
$defaultvalue = 'Nigeria'; //Please set default value here when nothing is selected before submit - change Nigeria
foreach($states->result() as $row)
     {
     echo "<option value='$row->state_name' " . set_select('state', $row->state_name, ((!isset(set_select('state', $row->state_name)) &&  ($row->state_name == $defaultvalue) ) ? TRUE : FALSE )) . " >". $row->state_name."</option>";
     }  ?>


</select>

如果您不想设置默认值,只需从set_select中删除TRUE

Just remove TRUE from set_select if you don't want to set a default value

如果使用菜单,此功能允许您显示所选的菜单项。第一个参数必须包含选择菜单的名称,第二个参数必须包含每个项的值,第三个(可选)参数允许您将项设置为默认值(使用布尔值TRUE / FALSE)。

If you use a menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

这篇关于如何在表单验证失败后重新填充codeigniter中的下拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆