bla*_*elt 6 codeigniter codeigniter-2
我正在使用Codeigniter的验证类来验证我的表单.如果发现任何验证错误,您能否告诉我如何从控制器重定向到上一页?
在我的控制器中:
if ($this->form_validation->run() == FALSE){
//**** Here is where I need to redirect
} else {
// code to send data to model...
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*lay 11
我为此扩展了URL帮助程序.
https://github.com/jonathanazulay/CodeIgniter-extensions/blob/master/MY_url_helper.php
在你的控制器中:
$this->load->helper('url');
redirect_back();
Run Code Online (Sandbox Code Playgroud)
只是把MY_url_helper.php在application/helpers,你是好去.
UPDATE
您希望发布表单,验证它,然后在验证失败时再次显示验证错误,或者在验证通过时显示完全不同的内容.
执行此操作的最佳方法是将表单发布回自身.所以你的表格的行动将是action="".这样,在您的方法中,您可以检查表单是否已提交,并确定在那里执行的操作:
// in my form method
if ($this->input->post('submit')) // make sure your submit button has a value of submit
{
// the form was submitted, so validate it
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
else
{
// the form wasn't submitted, so we need to see the form
$this->load->view('myform');
}
Run Code Online (Sandbox Code Playgroud)
老答复
您始终可以在表单中的隐藏字段中传递当前URI:
<input name="redirect" type="hidden" value="<?= $this->uri->uri_string() ?>" />
Run Code Online (Sandbox Code Playgroud)
如果验证失败,则重定向:
redirect($this->input->post('redirect'));
Run Code Online (Sandbox Code Playgroud)
或者您可以在flashdata会话变量中设置重定向网址:
// in the method that displays the form
$this->session->set_flashdata('redirect', $this->uri->uri_string());
Run Code Online (Sandbox Code Playgroud)
如果验证失败,则重定向:
redirect($this->session->flashdata('redirect'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23181 次 |
| 最近记录: |