简体   繁体   English

Form_dropdown中的CodeIgniter select_value

[英]CodeIgniter select_value in form_dropdown

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? 我做错了什么或错过了什么?

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);
?>

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提到的那样,您可以将数组作为多个选择的第3个参数传递。

If you want to use set_value for a specific field, you need to have a validation rule for that field. 如果要对特定字段使用set_value,则需要为该字段设置验证规则。 No validation rule makes set_value return NULL (or an empty string I have not checked which) 没有验证规则使set_value返回NULL(或者我没有检查空字符串)

In my controller I added 在我的控制器中我添加了

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

before my form_validation->run line. 在我的form_validation-> run line之前。 I could then used your syntax: 然后我可以使用你的语法:

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

I found set_value() worked, NOT set_select(). 我发现set_value()工作,而不是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. 请记住,您可以向set_value添加第二个参数,使其具有默认的男性或女性,如果表单尚未经过验证,则使用该参数。 I extract the existing value from my backend database, and pass this in as such: 我从后端数据库中提取现有值,并将其传递给:

<?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'). 另请注意,set值返回选项数组的索引('M'或'F'),而不是显示的值('Male'或'Female')。 For lists containing more options that two genders, I use the primary key of the database table containing the options, to ensure uniqueness. 对于包含两个性别的更多选项的列表,我使用包含选项的数据库表的主键来确保唯一性。

Although I wonder when I will first be asked to add 'Other' to the list of choices for gender.... 虽然我想知道我什么时候会被要求将“其他”添加到性别选择列表中....

With form_dropdown , you have to use set_select instead 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_dropdown function. 如果要在form_dropdown上设置默认值, form_dropdown包含默认值的数组作为第三个参数传递给form_dropdown函数。

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_checkbox and set_radio have 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有一些问题,似乎无法处理旨在处理BOTH创建和更新表单的表单。

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
        }
    }
}
?>

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'));?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM