重新填充字段,你可以使用SET_VALUE功能。
set_value()
允许你设置的输入形式或textarea的价值。您必须通过函数的第一个参数提供字段名称。第二个(可选)参数允许您为表单设置默认值。
首先检查表单验证和上传是否成功。
如果两者都成功,我们将用户重定向到新的一页。
如果其中一个失败,我们会向您的数据数组添加错误消息,我们可以在我们的视图中访问并显示我们的表单。
控制器
public function signup()
{
// Data array
$data = array();
// Load form validation libary
$this->load->library('form_validation');
// Load upload library and set configuration
$config['upload_path'] = './uploads/';
$this->load->library('upload', $config);
// Set the required fields
$this->form_validation->set_rules('first_name', 'First name', 'required');
if ($this->form_validation->run() == TRUE)
{
// If upload was succesfull
if ($this->upload->do_upload())
{
$upload_data = $this->upload->data();
// Build array to store in database
$save_data = array(
'first_name' => $this->input->post('first_name'),
'image' => $upload_data['file_name']
);
// Send data to your model to process
$this->your_model->save($save_data);
// Redirect to success page
redirect('registration_succes');
}
else
{
// Upload failed, set error
$data['error'] = $this->upload->display_errors();
}
}
else
{
// Form validation failed, set error
$data['error'] = validation_errors();
}
// Display the form by default or on error
$this->load->view('myform', $data);
}
在我们看来,我们重新填充使用SET_VALUE功能提交值的字段。
视图(myForm的)
<?php echo form_open_multipart('signup');?>
<fieldset>
<?php if(isset($error) && ! empty($error)): ?>
<div class="error"><?php echo $error; ?></div>
<?php endif; ?>
<p>
<label>First name</label>
<input type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" />
</p>
<p>
<label>File</label>
<input type="file" name="userfile" size="20" />
</p>
<p>
<input type="submit" />
</p>
</fieldset>
</form>
您的意思是使用表单来上传文件,然后访问与'$ _POST'变量文件? – hjpotter92
Yup codeigniter有这样做的方式...我的问题是我可以保留POST数据的副本,以防图像出错并且没有空字段值? –
你的控制器是否打开原始表单是第一个文件不被接受,或者是与form_upload不同的形式让用户选择新文件? – Tom