Search code examples
phpcodeignitercache-expiration

Document Expired Back Browser Pressed When Searching in Codeigniter


I have a program where i have searching code. When i do searching it will show a page with searching result. That page will display some content that can be click. When i click that content it will display me the detail of the product. From this detail page, when i clicked back in the browser it should be back to the search result page but i get document expired here. When i reload the page with document expired it will show the searching result again. I'm using codeigniter. I had try some code that i found but it still not working. That's my problem. Please help me. Thank you.

Controller

public function search_bar($keyWordRestoran=NULL, $page=NULL, $orderBy=NULL)
        {
            if($this->session->userdata('logged_in'))
            {
                $session_data = $this->session->userdata('logged_in');
                $data['nama'] = $session_data['nama'];
                $data['id'] = $session_data['id_user'];
                $data['tipeUser'] = $session_data['tipe_user'];

            }
            else{
                $data['nama'] = "";
                $data['id'] = "0";
                $data['tipeUser']="";
            }
            $data['url_image'] = $this->imageUrl;
            $keyWordRestoran_ip = $this->input->post('input-keyword');
            if($keyWordRestoran_ip=="")
            {
                $session_data = $this->session->userdata('keyword_resto');
                $keyWordRestoran = $session_data['keyword_resto'];
            }
            else{
                $sess_array2 = array();
                $sess_array2 = array(
                    'keyword_resto' => $keyWordRestoran_ip
                    );
                $this->session->set_userdata('keyword_resto', $sess_array2);
                $session_data = $this->session->userdata('keyword_resto');
                $keyWordRestoran = $session_data['keyword_resto'];
            }

            $data['kunci_cari'] = $keyWordRestoran;

            $config['base_url'] = base_url().'/home_controller/search_bar/'.$keyWordRestoran;

            $data['jmlh_rows'] = $this->restoran->GetRestoran_search($keyWordRestoran);

            $total_row = $data['jmlh_rows']->num_rows();

            $config["total_rows"] = $total_row;
            $config["per_page"] = 8;
            $config['cur_tag_open'] = '&nbsp;<a class="current">';
            $config['cur_tag_close'] = '</a>';
            $config['next_link'] = 'Next';
            $config['prev_link'] = 'Previous';

            $this->pagination->initialize($config);

            $str_links = $this->pagination->create_links();
            $data["links"] = explode('&nbsp;',$str_links );
            $data["dataRestoran"] = $this->restoran->GetRestoranPerPage_search($config["per_page"], $keyWordRestoran, $page, $orderBy);
            $data['url_image'] = $this->imageUrl;
            $data['data_kategori'] = $this->jenismakanan->Getjenismakanan();
            $this->load->view('food-type_search', $data);
        }

Search View

<div class="header-bottom">
    <div class="col-xs-1">
        <button class="toggle-button"></button>
    </div>
    <div class="col-xs-11">
    <?php echo form_open('home_controller/search_bar');?>
    <div class="search">
    <form method="GET">
        <input type="text" id= "input-keyword" name="input-keyword" placeholder="Search ..." value="<?php echo set_value('input-keyword')?>" >
        <input type="submit" value="">  
    </form><?php echo form_close(); ?> 
    </div>
    </div>
<div class="clearfix"> </div>


Solution

  • You have included form two times. One form is created using <?php echo form_open('home_controller/search_bar');?> and another is static in html.So need to remove static html form and for_open is ok. In form_open you need to specified the method get. Here i have updated this code-

    <div class="header-bottom">
        <div class="col-xs-1">
            <button class="toggle-button"></button>
        </div>
        <div class="col-xs-11">
        <?php echo form_open('home_controller/search_bar',array('method' => 'get'));?>
        <div class="search">
    
            <input type="text" id= "input-keyword" name="input-keyword" placeholder="Search ..." value="<?php echo set_value('input-keyword')?>" >
            <input type="submit" value="">  
        </div>
       <?php echo form_close(); ?> 
        </div>
    <div class="clearfix"> </div>
    

    At you controller in search_bar function now get form value using get method instead of post like as

    $keyWordRestoran_ip = $this->input->get('input-keyword');