dongyun51582 2015-05-19 17:38
浏览 42

通过页面刷新删除表单验证错误

I have a page that takes form input, it is written using HTML and PHP(codeignter framework), the issue I am facing is when there is a form validation error, the page refresh is not reloading the page like brand new, I mean its not clearing the input from fields and the error message still coming up on page refresh and even after I correct the input and hit submit, when I click browser back button the it is still loading the page with validation error.. Any idea why it happens or how to resolve it. I just need the page to reload when I refresh it.

Here is my view (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');
    }       
}

when the input validation fails, the url comes back as www.something.com/search/search_fn and when I refresh, it basically loading the same page with same error message. I, somehow, need to reset the $this->input->post('search'), so that it will be true only when the search button is clicked.

Thanks.

  • 写回答

1条回答 默认 最新

  • duandou2763 2015-05-19 18:22
    关注

    Are you using CodeIgniter's redirect() function after processing form? you should always do that

    I mean after you are done handling submitted data, instead of return TRUE or simply closing function, to a simple redirect('CONTROLLER_NAME'); even if you want them to be on the same page

    As for the fields, i would suggest using autocomplete="off" in your inputs (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)

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?