CodeIgniter select_value in form_dropdown

2020-02-11 08:36发布

问题:

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

Here is my code

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

What am I doing wrong or missing?

回答1:

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


回答2:

Remove the 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.



回答3:

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)

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:

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

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

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



回答4:

With form_dropdown, you have to use set_select instead of set_value

CodeIgniter documentation

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.



回答5:

CodeIgniter has set_checkbox() and set_radio() that you need to use instead of 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.

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

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


回答6:

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