CodeIgniter select_value in form_dropdown

I have a drop down box in my form, and when I click the submit button, the form passes the check, and if an error occurs, return the entire value. This work will be expected for my dropdown meu, I have a set_value value in the drop-down list, but it does not work :(

Here is my code

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

What am I doing wrong or missing?

+3
source share
6 answers

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);
?>
+4
source

Remove set_value('gender')

How in:

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

As Trung mentioned, you can pass an array as the third parameter for multiple selections.

+1
source

form_dropdown set_select set_value

CodeIgniter

form_dropdown, , form_dropdown.

0

CodeIgniter set_checkbox() set_radio(), set_value()

set_checkbox set_radio , , , .

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

, .

:

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

:

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

, , , :

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

set_value , . set_value return NULL ( , )

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

form_validation- > run. :

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

set_value(), set_select(). , , set_value , , . - :

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

Note that the given value returns the index of the options array ('M' or 'F'), and not the displayed value ('Male' or 'Female'). For lists containing more parameters that are used by two gender groups, I use the primary key of the database table containing the parameters to ensure uniqueness.

Although I’m curious when they first ask me to add β€œOther” to the list of options for gender ....

0
source

All Articles