如何通过redirect()传递验证错误数据?
我有一个包含表单的页面,当任何用户提交它时,数据都会发送到控制器,控制器检查验证,如果有任何错误,它将用户重定向到上一页(包含表单的页面),否则它将数据发送到模型。
要从控制器重定向到上一页(如果有任何验证错误),我有以下代码
redirect($this->input->post('redirect'));
上面的代码工作正常,但问题是在它将用户重定向到上一页(包含表单的页面)之后不显示验证错误。
那么,请您告诉我如何通过我上面发布的这个“重定向”代码传递验证错误信息,并在该页面上显示验证错误消息?
提前致谢:)
解决方案:
在我的控制器中:
$redirect=$this->input->post('redirect'); // << for this I have- <input name="redirect" type="hidden" value="<?= $this->uri->uri_string() ?>" /> in my view file
$this->session->set_flashdata('errors', validation_errors());
redirect($this->input->post('redirect'));
在我的视图文件中:
<?php
if ($this->session->flashdata('errors')){ //change!
echo "<div class='error'>";
echo $this->session->flashdata('errors');
echo "</div>";
}
?>
因此我可以通过从控制器重定向到上一页来传递验证错误数据
I have a page that contains a form and when any user submits it, the data goes to a controller and the controller checks the validation, if there is any error it redirects the user to the previous page( the page that contains the form), otherwise it sends data to models.
To redirect to the previous page from controller (if there is any validation error), I have the following code
redirect($this->input->post('redirect'));
The above code is working fine but the problem is after it redirects the user to the previous page(The page that contains the form) it is not displaying the validation errors.
So would you please kindly tell me how to pass the validation error information through this "redirect" code I posted above and show the validation error message on that page?
Thanks in Advance :)
Solution:
In my controller:
$redirect=$this->input->post('redirect'); // << for this I have- <input name="redirect" type="hidden" value="<?= $this->uri->uri_string() ?>" /> in my view file
$this->session->set_flashdata('errors', validation_errors());
redirect($this->input->post('redirect'));
In my view file:
<?php
if ($this->session->flashdata('errors')){ //change!
echo "<div class='error'>";
echo $this->session->flashdata('errors');
echo "</div>";
}
?>
Thus I can pass validation error data through redirect from controller to previous page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试 CI 会话库中的 flashdata。它使数据可用于下一个服务器请求。
You can try flashdata from CI's session library. It makes the data available for the next server request.