我有一个采用表单输入的页面,它是使用HTML和PHP(codeignter框架(编写的,我面临的问题是当出现表单验证错误时,页面刷新不会像全新的一样重新加载页面,我的意思是它不会清除字段中的输入,错误消息仍然会在页面刷新时出现,即使在我更正输入并点击提交之后,当我单击浏览器返回按钮时,它仍在加载验证错误的页面。。知道为什么会发生这种情况或如何解决它。我只需要刷新页面时重新加载。
这是我的视图(view1.php(
<html>
<?php
echo validation_errors();
form_open('search_controller/search_fn');
form_input('input1', set_value('input1'));
form_input('input2', set_value('input2'));
form_input('input3', set_value('input3'));
form_submit('submit', 'Submit');
?>
</html>
Controller Functions
function index() {
$this->load->view('view1');
}
function search_fn {
if($this->input->post('submit') {
$this->form_validation->set_rules('input1', 'XXXX',
'trim|some_validation');
$this->form_validation->set_rules('input2', 'XXXX',
'trim|some_validation');
$this->form_validation->set_rules('input3', 'XXXX',
'trim|some_validation');
if($this->form_validation->run() == FALSE) {
//when validation fails
$this->load->view('view1');
} else {
//when all input is validated sucessfully
do some processlisng,
//load some other view
$this->load->view('view2');
}
} else {
//When page is refreshed, this is supposed to redirect
redirect('search_controller/index');
}
}
当输入验证失败时,url返回为www.something.com/search/search_fn,当我刷新时,它基本上加载了带有相同错误消息的同一页面。I、 不知怎么的,需要重置$this->input->post('search'(,因此只有当单击搜索按钮时才会为true。
谢谢。
在处理表单后是否使用CodeIgniter的redirect()
函数?你应该一直做
我的意思是,在您处理完提交的数据后,而不是返回TRUE或简单地关闭函数,返回到一个简单的redirect('CONTROLLER_NAME');
,即使您希望它们在同一页面上
至于字段,我建议在输入(if you are not using an <input type="text"> above an <input type="password"> because Chrome and next version of Firefox will ignore automplete="off" in that situation)
中使用autocomplete="off">