Eka Soedono
Eka Soedono

Reputation: 41

How to keep value after submit

I would like to keep the value after submiting(search box), so I use set_value();
and I keep the value.
but after that I click the link(pagination) that directing me to the next page, I can't keep the value because I didn't click the submit button, so how can I keep the value without clicking submit button again ?

View

<?php
    $attr_form= array(
    'class'     => 'form-horizontal',
    'role'      => 'form'           
    );
    echo form_open('dipanddig/search', $attr_form);
    echo '<div class="input-group search-box-width">';
    echo form_input('search', set_value('search'), 'class="form-control search-box-height"');
    echo '<span class="input-group-btn">';
    echo '<button type="submit" class="btn btn-default search-box-height">';
    echo '<span class="glyphicon glyphicon-search">';
    echo '</span>';
    echo '</button>';
    echo '</span>';
    echo form_close();
    echo '</div>';
?>

Controller

    function search(){

        $this->load->library('pagination');

        $data['base_url']   = 'http://localhost:100/dipanddig/dipanddig/search';
        $data['total_rows'] = $this->db->select('name')->like('name', $this->input->post('search'))->get('clothes')->num_rows();
        $data['per_page']   = 1;
        $data['num_links']  = 2;
        $data['records']    = $this->db->select('id,price,name')->like('name', $this->input->post('search'))->get('clothes',$data['per_page'],$this->uri->segment(3));

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

        $this->load->view('products', $data);   
    }   

Thank you so much for helping me.

Upvotes: 0

Views: 920

Answers (1)

WebHQ
WebHQ

Reputation: 711

  1. After submitting a form you must "remember" a query. Use session for that.
  2. Inside search method use query value from session
  3. Add "Clear" button to search form

    function search()
    {
        $this->load->library('pagination');
        // add this
        $this->load->library('session');
        if ($this->input->post('search')) {
            $this->session->set_userdata('search' => $this->input->post('search'));
        }
    
        $data['base_url']   = 'http://localhost:100/dipanddig/dipanddig/search';
        $data['total_rows'] = $this->db->select('name')->like('name', $this->session->userdata('search'))->get('clothes')->num_rows();
        $data['per_page']   = 1;
        $data['num_links']  = 2;
        $data['records']    = $this->db->select('id,price,name')->like('name', $this->session->userdata('search'))->get('clothes',$data['per_page'],$this->uri->segment(3));
    
        $this->pagination->initialize($data);
        $this->load->view('products', $data);   
    }
    

HTTP is a stateless protocol. Try to get familiar with convention.

ps. You may also set a form value from session to get better user expirience

Upvotes: 1

Related Questions