通过页面刷新

时间:2015-05-19 17:38:34

标签: php forms codeigniter validation

我有一个页面,它采用表单输入,它是使用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');
    }       
}

当输入验证失败时,网址会返回www.something.com/search/search_fn,当我刷新时,它基本上会加载相同的页面并显示相同的错误消息。不知何故,我需要重置 $ this-&gt; input-&gt; post(&#39; search&#39;),只有在点击搜索按钮时才会成立。

感谢。

1 个答案:

答案 0 :(得分:0)

您在处理表单后使用的是CodeIgniter的redirect()函数吗?你应该总是这样做

我的意思是在您完成处理提交的数据之后,即使您希望它们位于同一页面上,也可以简单redirect('CONTROLLER_NAME');返回TRUE或简单地关闭函数

至于字段,我建议使用autocomplete =&#34; off&#34;在您的输入(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)

相关问题