I have a form in codeigniter and would like to have a default value for my input as well as the set_value().
我在codeigniter中有一个表单,并希望我的输入和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'?
我有一个设定值并且正在工作,但是如何添加默认值'12'?
2 个解决方案
#1
10
You can set a default if the value is empty.
如果值为空,则可以设置默认值。
From the codeigniter site:
从codeigniter网站:
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:
允许您设置输入表单或textarea的值。您必须通过函数的第一个参数提供字段名称。第二个(可选)参数允许您为表单设置默认值。例:
<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.
首次加载时,上述表格将显示“12”。
#2
0
Another Scenario where you can use both set_value and default value:
另一个场景,您可以使用set_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.
set_value()函数只是设置值。当您提交包含验证错误的表单时,此函数对于保留输入值更有用。这样用户无需重新输入字段。此函数具有第二个可选参数,允许您为表单设置默认值。
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.
如果要在表单中添加值,上面的语句将设置值。如果您正在编辑表单,它只显示数据库中捕获的值或发布数据。
#1
10
You can set a default if the value is empty.
如果值为空,则可以设置默认值。
From the codeigniter site:
从codeigniter网站:
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:
允许您设置输入表单或textarea的值。您必须通过函数的第一个参数提供字段名称。第二个(可选)参数允许您为表单设置默认值。例:
<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.
首次加载时,上述表格将显示“12”。
#2
0
Another Scenario where you can use both set_value and default value:
另一个场景,您可以使用set_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.
set_value()函数只是设置值。当您提交包含验证错误的表单时,此函数对于保留输入值更有用。这样用户无需重新输入字段。此函数具有第二个可选参数,允许您为表单设置默认值。
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.
如果要在表单中添加值,上面的语句将设置值。如果您正在编辑表单,它只显示数据库中捕获的值或发布数据。