3

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?

user1269625
  • 3,121
  • 26
  • 79
  • 111

6 Answers6

4

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);
?>
theftprevention
  • 5,083
  • 3
  • 18
  • 31
user1269625
  • 3,121
  • 26
  • 79
  • 111
1

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.

brightintro
  • 1,016
  • 1
  • 11
  • 16
1

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

NULL pointer
  • 1,116
  • 1
  • 14
  • 27
0

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.

Hieu Le
  • 8,288
  • 1
  • 34
  • 55
  • I tried this `` and it still did not work – user1269625 Jan 21 '13 at 19:56
  • Sorry, the `set_select` function works for in-HTML code, if you want to set the default value by the `form_dropdown` function, use the third parameter. – Hieu Le Jan 21 '13 at 20:01
  • You can read in the documentation above, in the `form_dropdown` section. They use the `$shirts_on_sale` array or the `'large'` value to set the default value of the `form_dropdown` – Hieu Le Jan 21 '13 at 20:04
0

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
        }
    }
}
?>
pbarney
  • 2,529
  • 4
  • 35
  • 49
0

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'));?>
Yuri Giovani
  • 119
  • 1
  • 3