php CodeIgniter 中的 set_value() 默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3873601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set_value() default in CodeIgniter
提问by Robimp
I've used formigniter to generate a form for CI. http://formigniter.org/
我已经使用 formigniter 为 CI 生成了一个表单。http://formigniter.org/
That bit works great. However I want to set a default value for the name field.
那一点效果很好。但是我想为 name 字段设置一个默认值。
The input code looks like this:
输入代码如下所示:
<label for="forename">Forename</label>
<?php echo form_error('forename'); ?>
<br /><input id="forename" type="text" name="forename" maxlength="255" value="<?php echo set_value('forename'); ?>" />
and I'd want to add in the first name with $this->session->userdata('current_client');
我想在名字中添加 $this->session->userdata('current_client');
Will it break my database insert if I just drop it into the set_value function?
如果我只是将它放入 set_value 函数,它会破坏我的数据库插入吗?
Edit:
编辑:
Sorry I don't think I was very clear there. I want the name field to be automatically filled in with the name from the session cookie.
抱歉,我认为我在那里不是很清楚。我希望使用会话 cookie 中的名称自动填充名称字段。
回答by bradym
As long as you're properly escaping input data before running the query, it shouldn't cause any problems. The set_value function just sets the value, the only benefit to using it is it simplifies setting the value to an already submitted value when redisplaying the form or showing a default value when the form has yet to be submitted.
只要您在运行查询之前正确转义输入数据,就不会造成任何问题。set_value 函数只是设置值,使用它的唯一好处是它简化了在重新显示表单时将值设置为已经提交的值或在表单尚未提交时显示默认值的过程。
This would use the session var as the default value for the form field:
这将使用会话变量作为表单字段的默认值:
<input id="forename" type="text" name="forename" maxlength="255" value="<?php echo set_value('forename', $this->session->userdata('current_client')); ?>" />
回答by vikas
Same case with I case
与我的情况相同的情况
$data = array(
'name' => 'qty_' . $i,
'size'=>15,
'id' => 'qty_' . $i,
'required'=>'required',
'class'=>'input-small',
'value' => set_value('qty_' . $i),
$restock_thirty
);
echo form_input($data);
回答by pradip kor
In CodeIgniter, if generate an error in controller code suppose username unfilled then return an error but the page can refresh, then set_value can set that text box value
在 CodeIgniter 中,如果在控制器代码中生成错误假设用户名未填充则返回错误但页面可以刷新,然后 set_value 可以设置该文本框值
<input type="text" name="username" placeholder="Username.." value="<?php echo set_value('username') ?>" class="form-control tx">