php form_dropdown 中的 CodeIgniter select_value

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14445982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:15:51  来源:igfitidea点击:

CodeIgniter select_value in form_dropdown

phpcodeigniterdrop-down-menu

提问by user1269625

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 :(

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

Here is my code

这是我的代码

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

What am I doing wrong or missing?

我做错了什么或错过了什么?

回答by user1269625

Doing this worked well:

这样做效果很好:

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

回答by brightintro

Remove the set_value('gender')

去除那个 set_value('gender')

As in:

如:

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

As Trung was mentioning you can pass an array as the 3rd parameter for multiple selections.

正如 Trung 所提到的,您可以将数组作为多个选择的第三个参数传递。

回答by NULL pointer

If you want to use set_value for a specific field, you need to have a validation rule for that field. No validation rule makes set_value return NULL (or an empty string I have not checked which)

如果要对特定字段使用 set_value,则需要为该字段设置验证规则。没有验证规则使 set_value 返回 NULL(或我没有检查过的空字符串)

In my controller I added

在我的控制器中,我添加了

$this->form_validation->set_rules('gender','Gender','required');

before my form_validation->run line. I could then used your syntax:

在我的 form_validation->run 行之前。然后我可以使用你的语法:

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

I found set_value() worked, NOT set_select(). Remember you can add a second parameter to set_value to have a default of male or female which is used if the form has not yet been validated. I extract the existing value from my backend database, and pass this in as such:

我发现 set_value() 有效,而不是 set_select()。请记住,您可以将第二个参数添加到 set_value 以具有默认的男性或女性,如果表单尚未验证,则使用该参数。我从我的后端数据库中提取现有值,并将其传递如下:

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

Note also that set value returns the index of the array of options('M' or 'F'), not the displayed value ('Male' or 'Female'). For lists containing more options that two genders, I use the primary key of the database table containing the options, to ensure uniqueness.

另请注意,set value 返回选项数组的索引('M' 或 'F'),而不是显示值('Male' 或 'Female')。对于包含两个性别的更多选项的列表,我使用包含选项的数据库表的主键,以确保唯一性。

Although I wonder when I will first be asked to add 'Other' to the list of choices for gender....

虽然我想知道什么时候我会第一次被要求将“其他”添加到性别选择列表中......

回答by Hieu Le

With form_dropdown, you have to use set_selectinstead of set_value

使用form_dropdown,您必须使用set_select代替set_value

CodeIgniter documentation

CodeIgniter 文档

If you want to set the default value on form_dropdown, pass an array which contains defaults value as the third parameter to the form_dropdownfunction.

如果要在 上设置默认值form_dropdown,请将包含默认值的数组作为第三个参数传递给form_dropdown函数。

回答by pbarney

CodeIgniter has set_checkbox()and set_radio()that you need to use instead of set_value()

CodeIgniter 具有set_checkbox()并且set_radio()您需要使用而不是set_value()

But set_checkboxand set_radiohave some issues, and don't seem to be able to handle forms that are designed to handle BOTH create AND update forms.

但是,set_checkboxset_radio有一些问题,并且似乎没有能够处理被设计为同时处理创建和更新表单形式。

This is the fix. You can put this code in a helper or extend the form_validation class.

这是修复。您可以将此代码放在帮助程序中或扩展 form_validation 类。

<?php echo form_dropdown('gender', $gender, set_it('gender','M',$person)); ?>

<?php
/*   $field is the field you're setting
 *   $value is the "selected" value from the previous form post
 *   $defaults is an object containing defaults in case the form is being used to create a new record. It could be filled with null values, or actual default values if you need that. 
 */
function set_it($field, $value, $defaults = null)
{
    // first, check to see if the form element was POSTed
    if (isset($_POST[$field]))
    {
        // does it match the value we provided?
        if ($_POST[$field] == $value)
        {
            // yes, so set the checkbox
            echo "checked='checked'"; // valid for both checkboxes and radio buttons
        }
    }
    // There was no POST, so check to see if the provided defaults contains our field
    elseif ( ! is_null($defaults) && isset($defaults->$field))
    {
        // does it match the value we provided?
        if ($defaults->$field == $value)
        {
            // yes, so set the checkbox
            echo "checked='checked'"; // valid for both checkboxes and radio buttons
        }
    }
}
?>

回答by Yuri Giovani

Probably you are not validating the form.

可能您没有验证表单。

Use this:

用这个:

$this-> form_validation-> set_rules ('gender', 'Label', 'xss_clean');

To use:

使用:

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

If not please use the form validation, do as follows:

如果不是,请使用表单验证,请执行以下操作:

<? php echo form_dropdown ('gender', $ gender, $ this-> input-> post ('gender'));?>