[英]codeigniter set_select can't get data from session
I'm trying to post a value from a title data which came from my session. 我正在尝试从来自会话的标题数据中发布一个值。 other input fields are working except select, radio button, checkbox function is not showing the value.
除选择,单选按钮,复选框功能未显示该值外,其他输入字段均有效。
Controller 控制者
function index(){
$data = $this->get_data_from_session();
$this->load->view('edit_profile', $data);
}
function get_data_from_post(){
$data['title'] = $this->input->post('title', TRUE);
$data['firstname'] = ucwords(strtolower($this->input->post('firstname', TRUE)));
return $data;
}
function get_data_from_session(){
$data['title'] = $this->session->userdata('title');
$data['firstname'] = $this->session->userdata('firstname');
return $data;
}
function update(){
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[2]|xss_clean');
if ($this->form_validation->run($this) == FALSE){
$data = $this->get_data_from_post();
$this->load->view('edit_profile',$data);
}else{
//update db
}
}
View File (edit_profile.php) 查看文件(edit_profile.php)
<?php echo form_open('update'); ?>
<select name="title" class="form-control">
<option value="" <?php echo set_select('title', ''); ?> >Title</option>
<option value="Dr" <?php echo set_select('title', 'Dr'); ?> >Dr</option>
<option value="Mr" <?php echo set_select('title', 'Mr'); ?> >Mr</option>
<option value="Mrs" <?php echo set_select('title', 'Mrs'); ?> >Mrs</option>
<option value="Ms" <?php echo set_select('title', 'Ms'); ?> >Ms</option>
</select>
<input type="text" class="form-control" id="fname" placeholder="* First Name" name="firstname" value="<?php echo $firstname; ?>" autofocus autocomplete="off">
<button type="submit" class="btn btn-primary">UPDATE INFORMATION</button>
<?php echo form_close(); ?>
firstname is showing the value but the title is not. 名字显示值,但标题不显示。 Can anybody help me with this problem?
有人可以帮我解决这个问题吗? Thank you in advance.
先感谢您。
if ( ! function_exists('set_select'))
{
function set_select($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 AND $default == TRUE)
{
return ' selected="selected"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR ($field != $value))
{
return '';
}
}
return ' selected="selected"';
}
return $OBJ->set_select($field, $value, $default);
}
}
This is the CI set_select function which expects $field to be supplied with post method. 这是CI set_select函数,它期望$ field与post方法一起提供。 and here you aren't, you are supplying through session.
而这里不是,而是通过会话进行供应。
First name is showing beause you are using value="<?php echo $firstname; ?>"
instead of value="<?php echo set_value('firstname'); ?>"
.Set_value for input fields is directly from post and is equivalent to set_select (of select field). 名字显示是因为您使用的是
value="<?php echo $firstname; ?>"
而不是value="<?php echo set_value('firstname'); ?>"
。输入字段的Set_value直接来自post和等同于(选择字段的)set_select。 Now check whether firstname appears using set_value(). 现在,使用set_value()检查名字是否出现。
problem solved :) i just include some line of codes 解决的问题:)我只包含一些代码行
<option value="Mr" <?php echo set_select('title', 'Mr', ($title == 'Mr')); ?> >Mr</option>
<option value="Mrs" <?php echo set_select('title', 'Mrs', ($title == 'Mrs')); ?> >Mrs</option>
this is what I include inside the set_select and it's also work with radio button. 这就是我在set_select中包含的内容,并且还可以使用单选按钮。
, ($title == 'Mr')
Alternatively you can do this.(as you are echoing the variable for firstname above) 或者,您也可以执行此操作。(因为您正在回显上面名字的变量)
<?php echo form_open('update'); ?>
<select name="title" class="form-control">
<option value="" <?php echo $title=='' ? 'selected="selected"' : '';?> >Title</option>
<option value="Dr" <?php echo $title=='Dr' ? 'selected="selected"' : '';?> >Dr</option>
<option value="Mr" <?php echo $title=='Mr' ? 'selected="selected"' : '';?> >Mr</option>
<option value="Mrs" <?php echo $title=='Mrs' ? 'selected="selected"' : '';?> >Mrs</option>
<option value="Ms" <?php echo $title=='Ms' ? 'selected="selected"' : '';?> >Ms</option>
</select>
<input type="text" class="form-control" id="fname" placeholder="* First Name" name="firstname" value="<?php echo $firstname; ?>" autofocus autocomplete="off">
<button type="submit" class="btn btn-primary">UPDATE INFORMATION</button>
<?php echo form_close(); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.