CodeIgniter select_value in form_dropdown

2021-12-28 00:00:00 drop-down-menu php codeigniter

我的表单中有一个下拉字段,当我点击提交时,表单会通过验证,如果发生错误,则返回所有值.这项工作期待我的下拉菜单,我在下拉菜单中有 set_value 但它不起作用:(

I have a dropdown field in my form and when I hit submit the form goes through validation and if an error happens, return all the value. This work expect for my dropdown meu, I have the set_value in the dropdown but it doesnt work :(

这是我的代码

<?php echo form_dropdown('gender', $gender, set_value('gender')); ?>

我做错了什么或遗漏了什么?

What am I doing wrong or missing?

推荐答案

这样做效果很好:

<?php
$selected = ($this->input->post('gender')) ? $this->input->post('gender') : 'M';  
$gender = array("M" => "Male", "F" => "Female");
echo form_dropdown('gender', $gender, $selected);
?>

相关文章