简体   繁体   English

提交表单后,CodeIgniter刷新页面

[英]CodeIgniter refresh page after form submit

I am trying to refresh a page via CodeIgniter, but I can't seem to get this to work. 我正在尝试通过CodeIgniter刷新页面,但似乎无法正常工作。

Basically, we have a list of jobs on a page and a user has the option to filter the said jobs. 基本上,我们在页面上有一个作业列表,用户可以选择过滤所述作业。

So, a user will select checkboxes and then click save to save their selections to a database based on their user id. 因此,用户将选择复选框,然后单击“保存”以根据其用户ID将其选择保存到数据库中。

I will obviously need the data to refresh once they have saved their selection, but I have tried many things and this doesn't seem to work at all. 一旦他们保存了选择,我显然需要刷新数据,但是我尝试了很多事情,但这似乎根本不起作用。 I assume I've got the redirect in the wrong place, which is currently in the controller. 我假设我将重定向放在错误的位置,该位置当前在控制器中。

I've tried the following: 我尝试了以下方法:

    $this->load->helper('url');

    redirect('dashboard', 'refresh');

But nothing happens. 但是什么也没发生。

I have the above redirect in the controller function: 我在控制器功能中具有上述重定向:

        public function save_filters() {    

        if (isset($_POST)){
            $filters = $_POST['client_ids'];
        }   

        $this->load->model('dashboard_model');  
        $this->dashboard_model->update_dashboard_filters($filters);

         $this->load->helper('url');

        redirect('dashboard', 'refresh');

    }

Did I put it in the wrong place? 我把它放错了地方吗?

I get no errors or warnings from firebug? 我没有收到来自萤火虫的错误或警告吗?

The form: 表格:

            <form id="filters_form">
            <table>
                <tr>
                    <td>
                        <label for="GAB Robins">GAB Robins </label><input type="checkbox" name="client_ids[]" value="4">
                    </td>
                    <td>
                        <label for="Cunningham Lindsay">Cunningham Lindsay </label><input type="checkbox" name="client_ids[]" value="5">
                    </td>
                    <td>
                        <label for="Lloyds">Lloyds </label><input type="checkbox" name="client_ids[]" value="7">
                    </td>
                    <td>
                        <label for="Sundry/Private">Sundry/Private </label><input type="checkbox" name="client_ids[]" value="9">
                    </td>
                    <td>
                        <label for="WNS Ltd">WNS Ltd </label><input type="checkbox" name="client_ids[]" value="4441">
                    </td>
                    <td>
                        <label for="Stream">Stream </label><input type="checkbox" name="client_ids[]" value="4493">
                    </td>
                    <td>
                        <label for="Redesign">Redesign </label><input type="checkbox" name="client_ids[]" value="5295">
                    </td>                                                                                                                       
                </tr>
            </table>
            <input type="button" value="Save selections" name="save_filters" id="save_filters_button" />
        </form>

The javascript that deals with the form 处理表单的javascript

        save_filter: function () {
        $.post('/dashboard/save_filters', $('#filters_form').serialize(), function (response) {
            if (response.status == 'ok') {
                if ($('.ui-success').length == 0) {
                    $('#page_main').before('<div class="ui-success" style="margin-bottom: 1em; font-weight: bold; border-bottom: 1px solid #ccc; padding: 1em;"><img src="/common/images/icons/accept.png" style="margin-right: 10px;" align="absmiddle" />Filters saved!</div>').fadeIn(1000);
                }
            }
            else {
                $(response.errors).each(function (i, item) {
                    alert(item);
                });
            }
        });
    }   

Put the action in form action="<?php base_url(); ?>controller_name/function_name" and method="post" 将动作放入以下形式: action="<?php base_url(); ?>controller_name/function_name"method="post"

Example: 例:

 <form id="filters_form"  action="<?php base_url(); ?>controller_name/function_name" method="post">

UPDATE: Because you are using JS, you are not refreshing page. 更新:因为您使用的是JS,所以您没有刷新页面。 Don't use JS if you want to refresh the page. 如果要刷新页面,请不要使用JS。

Its not working because you have an Ajax request, and you cant make redirect on server before you return data to JavaScript that made that request. 它不起作用,因为您有一个Ajax请求,并且在将数据返回到发出该请求的JavaScript之前,无法在服务器上进行重定向。

you can make redirect based on your response 您可以根据您的回复进行重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM