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

I have a form in codeigniter and I would like to have a default value for my input as well as set_value ().

This is my input:

echo form_input('job_position',set_value('job_position')); 

I have a set value in place and it works, but how can I add a default value of '12'?

+6
source share
2 answers

You can set the default value if the value is empty.

From codeigniter site :

set_value ()

Allows you to set the value of an input form or text field. You must enter the field name through the first parameter of the function. the second (optional) parameter allows you to set the default value for the form. Example:

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

In the above form, β€œ12” will be displayed on first boot.

+10
source

Another scenario where you can use both set_value and the default value:

The set_value() function simply sets the value. This function is more useful for saving input values ​​when submitting a form with validation errors. So the user does not need to re-enter the fields. This function has a second optional parameter, which allows you to set the default value for the form.

But if you want to fill in your default value if you use the same form to edit and add data.

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

The above statement sets the value if you add values ​​to the form. If you edit the form, it simply displays the captured value from the database or published data.

0
source

Source: https://habr.com/ru/post/917608/


All Articles