How to have both a default value and a set value in codeigniter forms? How to have both a default value and a set value in codeigniter forms? codeigniter codeigniter

How to have both a default value and a set value in codeigniter forms?


You can set a default if the value is empty.

From the codeigniter site:

set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:

<input type="text" name="job_position" value="<?php echo set_value('job_position', '12'); ?>" size="50" />

The above form will show "12" when loaded for the first time.


Another Scenario where you can use both set_value and default value:

The set_value() function just sets the value. This function is more useful to preserve the input values when you submit a form with validation errors. So that the user need not to re-enter the fields.This function has second optional parameter allows you to set a default value for the form.

But If you want to populate your default value if you are using the same form for both editing and adding the data.

<input type="text" name= "name" value = "<?php if($form['name']) echo $form['name']; else echo set_value('name');

The above statement will sets the value if you are adding the values in form. If you are editing the form it simply display the captured value from the database or post data.