0

I have a page with ajax pagination on it, I am currently able to check if the session exists and process accordingly. However, I cannot seem to remove the menu or reload the page properly if the session has expired. Only the menu remains and the login page shows in the small area where the table was.

Controller code

public function index()
    {
        $conditions = array();
        $data = array();
        $totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
        $config['target']      = '#list';
        $config['base_url']    = site_url('/AdminDocuments/Search');
        $config['total_rows']  = $totalRec;
        $config['per_page']    = $this->get_per_page();
        $this->ajax_pagination->initialize($config);
        $data['links'] = $this->ajax_pagination->create_links();
        $data['datatable'] = $this->DocumentModel->admin_get_and_search(array('limit'=>$this->get_per_page()));
        $data['user'] = $this->AccountModel->get_person($this->get_person_id());
        $data['current_page'] = $this->ajax_pagination->getCurrPage();
        $this->load->view('layout/admins/common/header');
        $this->load->view('layout/admins/common/navigation');
        $this->load->view('layout/admins/common/title');
        $this->load->view('layout/admins/common/errors');
        $this->load->view('layout/admins/common/search');
        $this->load->view('admins/documents/index',$data);
        $this->load->view('layout/admins/common/footer'); 
    }

    public function search(){
        if($this->input->is_ajax_request()){
            if(!$this->logged_in()){
                $this->index();
            }else{
                $conditions = array();
                $page = $this->input->post('page');
                if(!$page){
                    $offset = 0;
                }else{
                    $offset = $page;
                }
                $keywords = $this->input->post('keywords');
                if(!empty($keywords)){
                    $conditions['search']['keywords'] = $keywords;
                }
                $totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
                $config['target']      = '#list';
                $config['base_url']    = site_url('/AdminDocuments/Search');
                $config['total_rows']  = $totalRec;
                $config['per_page']    = $this->get_per_page();
                $this->ajax_pagination->initialize($config);
                $conditions['start'] = $offset;
                $conditions['limit'] = $this->get_per_page();
                $data['links'] = $this->ajax_pagination->create_links();
                $data['datatable'] = $this->DocumentModel->admin_get_and_search($conditions);
                $data['current_page'] = $this->ajax_pagination->getCurrPage();
                $this->load->view('admins/documents/ajax_pagination', $data, false);
            }
        }
    }

My JS Code that is placed in the view

<script>

function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    var keywords = $('#search').val();
    $.ajax({
        type: 'POST',
        url: 'url/AdminDocuments/Search/'+page_num,
        data:'page='+page_num+'&keywords='+keywords,
        beforeSend: function () {
            $('.loading').show();
        },
        success: function (html) {
            $('#list').html(html);
            $('.loading').fadeOut("slow");
        }
    });
}

function changeStatus(input){
    var id = input;
    $.ajax({
        type:'POST',
        url:'url/AdminDocuments/ChangeStatus/',
        data:'id='+id,
        beforeSend: function () {
            $('.loading').show();
        },
        success:function(result){
            console.log(result);
            searchFilter(0);
            $('.loading').fadeOut("slow");
        }
    });
}

function deleteDocument(input){
    var id = input;
    $.ajax({
        type:'POST',
        url:'url/AdminDocuments/Delete/',
        data:'id='+id,
        beforeSend: function () {
            $('.loading').show();
        },
        success:function(result){
            searchFilter(0);
            $('.loading').fadeOut("slow");
        }
    });
}
</script>
2
  • can you post your js code as well ? Commented Jun 23, 2017 at 3:37
  • Hey Yeah sure. My pagination code is loaded from the ajax pagination library and there is javascript there too. Commented Jun 23, 2017 at 3:46

1 Answer 1

1

i am assuming $('#list').html(html); code loads the html in the dom. instead of directly sending the html from php you can send a json containing the html as well the login status. like this.

$data = [
  'login_status' => 1 // or 0,
  'html' => $html // full html your are sending now
];

echo json_encode($data);

then in ajax success.

function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    var keywords = $('#search').val();
    $.ajax({
        type: 'POST',
        url: 'url/AdminDocuments/Search/'+page_num,
        data:'page='+page_num+'&keywords='+keywords,
        beforeSend: function () {
            $('.loading').show();
        },
        success: function (response) {
            var data = $.parseJSON(response);

        if(data.login_status == 0)
        {
          window.location.href = 'redirect to login page';
        }

        if(data.login_status == 1)
        {
          $('#list').html(data.html);
        }
            $('.loading').fadeOut("slow");
        }
    });
}

controller method :

public function search(){
        if($this->input->is_ajax_request()){

                $conditions = array();
                $page = $this->input->post('page');
                if(!$page){
                    $offset = 0;
                }else{
                    $offset = $page;
                }
                $keywords = $this->input->post('keywords');
                if(!empty($keywords)){
                    $conditions['search']['keywords'] = $keywords;
                }
                $totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
                $config['target']      = '#list';
                $config['base_url']    = site_url('/AdminDocuments/Search');
                $config['total_rows']  = $totalRec;
                $config['per_page']    = $this->get_per_page();
                $this->ajax_pagination->initialize($config);
                $conditions['start'] = $offset;
                $conditions['limit'] = $this->get_per_page();
                $data['links'] = $this->ajax_pagination->create_links();
                $data['datatable'] = $this->DocumentModel->admin_get_and_search($conditions);
                $data['current_page'] = $this->ajax_pagination->getCurrPage();
                $html = $this->load->view('admins/documents/ajax_pagination', $data, true);
    $res['html'] = $html;
    $res['login_status'] = ($this->logged_in()) ? '1' : '0';

       echo json_encode($res);

        }
Sign up to request clarification or add additional context in comments.

7 Comments

$('#list').html(html); loads the html in the dom. I have tried redirect('/Login') but the problem is that it only reloads the list container. Will window.location reload the entire screen?
yup it will reload the entire screen
redirect('/Login') in php, it will only send the login whole login page in response to ajax but if u do it after the ajax response i.e in success window.location it will reload the entire page.
Hi, thank you for helping. I have tried window.location.href but it still does the same thing. It reloads the container only for some reason.
did u do it in success block of ajax response ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.