Codeigniter:如何通过表单验证正确重定向
我知道如何在没有现有值的情况下使用普通表单进行操作,但假设我有一个可以通过调用的视图。假设我有两个字段,username、password和city,它们都是从数据库中提取的(当然除了password)。因此,如果用户试图提交表单,但由于任何原因验证失败,我应该将其“重定向”到哪里?现在,我让它显示相同的视图,但问题是,它再次从数据库中提取信息。我应该创建两种不同的视图吗Codeigniter:如何通过表单验证正确重定向,codeigniter,Codeigniter,我知道如何在没有现有值的情况下使用普通表单进行操作,但假设我有一个可以通过调用的视图。假设我有两个字段,username、password和city,它们都是从数据库中提取的(当然除了password)。因此,如果用户试图提交表单,但由于任何原因验证失败,我应该将其“重定向”到哪里?现在,我让它显示相同的视图,但问题是,它再次从数据库中提取信息。我应该创建两种不同的视图吗 第二个视图基本上会显示他们试图输入的信息以及错误消息。您不需要两个单独的视图。签出函数设置值(),设置选择(),设置复选框(
第二个视图基本上会显示他们试图输入的信息以及错误消息。您不需要两个单独的视图。签出函数
设置值()
,设置选择()
,设置复选框()
和设置收音机()
。在表单提交和验证后,这些重新填充表单。因此,在您的情况下,应按以下方式指定字段:
<input type="text"
name="username"
value="<?php echo set_value('username', $user['username']); ?>" />
<input type="text"
name="city"
value="<?php echo set_value('city', $user['city']); ?>" />
在同一个控制器上,您可以有如下内容:
if ($this->form_validation->run('create_comment') === TRUE)
{
$this->comments_model->name = $this->input->post('name', TRUE);
$this->comments_model->email = $this->input->post('email', TRUE);
$this->comments_model->website = $this->input->post('website', TRUE);
$this->comments_model->comment = $this->input->post('comment', TRUE);
$this->comments_model->create_comment();
redirect(current_url());
}
$this->load->view('your_view');
就这些
我们的想法是,当验证返回“true”时,让它重定向到自身或任何你想要的地方,这样我们就可以刷新页面,从而更新页面
如果验证返回“false”,则无需执行任何操作。重定向到同一表单 在您看来,向访问者提供错误信息 有两种方法可以做到这一点
”,“
”)...
...
$this->session->set_flashdata('msg', 'All fields are required. or other useful info here. Please try again!');
redirect('yourcontroller');
在你看来,你需要表现出来
<?php
if ($this->session->flashdata('msg')){ //change!
echo "<div class='message'>";
echo $this->session->flashdata('msg');
echo "</div>";
}
?>
也有同样的问题,并发现重定向会使您丢失form_error(…)或validation_errors()提供的数据,除非您将这些数据存储在会话或传递到加载视图的数组中
需要注意的一点是,仅当要传递的数据在会话中时才应该重定向,否则应该只加载一个视图。后者确保在到达加载的视图时,验证错误保持不变。如果表单验证失败,只需加载相同的视图即可
控制器
$userData=array(
'username'=NULL,
'password'=NULL
);
#set form validation rules
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
#get all posted data ,this helps you in two ways,
# 1. Get all form data and can be use here server side
# 2. repopulating the form data by passing to view page
$userData=$this->input->post(NULL, TRUE);
#check form validation result
if ($this->form_validation->run() == TRUE) {
//do the operation
redirect(URL);
}else{
$this->load->view($view, $userData);
}
查看页面
<form method=post action='URL'>
<input type='text' name='username'value='<?php echo $username?>'/>
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?>
<input type='text' name='password' value='<?php echo $password?>'/>
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?>
<input type='submit' value='submit'/>
</form>
<form method=post action='URL'>
<input type='text' name='username'value='<?php echo $username?>'/>
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?>
<input type='text' name='password' value='<?php echo $password?>'/>
<?php echo (form_error('username')) ? form_error('username', "<div style='color:red'>", "</div>") : ""; ?>
<input type='submit' value='submit'/>
</form>