我是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>
如果我的验证成功,它会正确保存到数据库,但在页面重新加载时不会填充文本框。如果存在验证错误,则数据库不会按预期更改,但值不会保留在表单字段中。
答案 0 :(得分:2)
变化
$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。
答案 1 :(得分:1)
问题是,当updateSeller()完成时,您不会将$ data ['seller']信息发送回视图。
这是一个重新设计的updateSeller()函数:
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'));
$data['seller'] = array();
$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'));
$data['seller'] = $seller; // send form data back to view
$this->seller_model->update_seller($id,$seller);
$data['message'] = '<div class="success">update seller success</div>';
}
$this->load->view('EditSeller', $data);
}