我是CodeIgniter的新手,在简单的CRUD应用程序中提交表单(错误与成功)后,我真的很难让表单值重新填充.我认为如果在此处发布代码可能会有所帮助,也许有人可以提供帮助.
这是从我的控制器来的:
function update($id){
$seller = $this->seller_model->get_seller_byID($id)->row();
$data['seller'] = $seller;
$data['title'] = 'Update seller';
$data['message'] = '';
$data['action'] = site_url('seller/updateSeller');
$data['link_back'] = anchor('seller/ManageSellers/','Back to list of sellers',array('class'=>'back'));
$this->load->view('EditSeller', $data);
}
function updateSeller(){
$data['title'] = 'Update seller';
$data['action'] = site_url('seller/updateSeller');
$data['link_back'] = anchor('seller/ManageSellers/','Back to list of sellers',array('class'=>'back'));
$this->setValidation();
if ($this->form_validation->run() == FALSE)
{
$data['message'] = 'fail';
}
else
{
$id = $this->input->post('id');
$seller = array('s_fname' => $this->input->post('fname'),
's_lname' => $this->input->post('lname'),
's_email' => $this->input->post('email'),
's_password' => $this->input->post('pword'));
$this->seller_model->update_seller($id,$seller);
$data['message'] = '<div class="success">update seller success</div>';
}
$this->load->view('EditSeller', $data);
}
function setValidation()
{
$this->form_validation->set_rules('fname', 'First Name', 'required');
$this->form_validation->set_rules('lname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('pword', 'Password', 'required');
$this->form_validation->set_rules('status', 'Status', 'required');
$this->form_validation->set_rules('dealer', 'Dealer', 'required');
}
这是我的看法:
<html>
<head>
</head>
<body>
<h2><?php echo $title; ?></h2>
<?php echo $message; ?>
<?php echo validation_errors(); ?>
<div class="content">
<form method="post" action="<?php echo $action; ?>">
<div class="data">
<p><?php
if (isset($seller->s_id))
{
echo "Id: " . $seller->s_id;
echo form_hidden("id", isset($seller->s_id) ? $seller->s_id : '' );
}
?></p>
<p>First Name:
<?php
$field = array( 'name' => 'fname',
'size' => 30,
'class'=>'input',
'value' => (isset($seller->s_fname) ? $seller->s_fname : '' ) );
echo form_input($field, set_value('fname'));
?>
</p>
<p>Last Name:
<?php
$field = array( 'name' => 'lname',
'size' => 40,
'class'=>'input',
'value' => (isset($seller->s_lname) ? $seller->s_lname : '' ) );
echo form_input($field, set_value('lname'));
?>
</p>
<p>Email:
<?php
$field = array( 'name' => 'email',
'size' => 60,
'class'=>'input',
'value' => (isset($seller->s_email) ? $seller->s_email : '' ) );
echo form_input($field, set_value('email'));
?>
</p>
<p>Password:
<?php
$field = array( 'name' => 'pword',
'size' => 20,
'class'=>'input',
'value' => (isset($seller->s_password) ? $seller->s_password : '' ) );
echo form_password($field, set_value('pword'));
?>
</p>
<p>Status:
<select name="status">
<option value="1" <?php echo set_select('status', '1', TRUE); ?>>Verified</option>
<option value="2" <?php echo set_select('status', '2'); ?>>Not verified</option>
</select>
</p>
<p>Dealer:
<input type="radio" name="dealer" value="Y" <?php echo set_radio('dealer', '1', TRUE); ?>/> Yes
<input type="radio" name="dealer" value="N" <?php echo set_radio('dealer', '2'); ?>/> No
</p>
<p><?php
if (isset($seller->s_startdate))
{
echo "Start Date: " . $seller->s_startdate;
}
?></p>
<input type="submit" value="Save"/>
<p><?php echo $link_back; ?></p>
</div>
</form>
<br />
</div>
</body>
</html>
如果我的验证成功,它将正确保存到数据库,但是在重新加载页面时,不会填充该文本框.发生验证错误时,数据库不会按预期进行更改,但值不会保留在表单字段中.
解决方法:
更改
$field = array( 'name' => 'fname',
'size' => 30,
'class'=>'input',
'value' => (isset($seller->s_fname) ? $seller->s_fname : '' ) );
echo form_input($field, set_value('fname'));
至
$field = array( 'name' => 'fname',
'size' => 30,
'class'=>'input',
'value' => set_value('s_fname', (isset($seller->s_fname) ? $seller->s_fname : '' )) );
echo form_input($field);
(并针对所有其他类似的情况执行相同的操作.
您可以了解有关set_value以及重新填充表格here的更多信息.