Reputation: 3311
I have a form in codeigniter and would like to have a default value for my input as well as the set_value().
This is my input:
echo form_input('job_position',set_value('job_position'));
I have a set value in place and working but how can I add a default value of '12'?
Upvotes: 6
Views: 10299
Reputation: 7515
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.
Upvotes: 0
Reputation: 7902
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.
Upvotes: 10