0

How to use GET method and Route it in Codeigniter? I have this :

index.php?search_term=somestring

How to get the search term value in a controller and how to make the URL clean like the following one :

search/somestring
Akash lal
  • 314
  • 6
  • 18
  • 1
    Have you read the documentation about routing? https://www.codeigniter.com/userguide3/general/routing.html. It goes in to this quite in depth. – M. Eriksson Jun 06 '18 at 13:57
  • Possible duplicate of [How to extract get variable from query string in codeigniter?](https://stackoverflow.com/questions/3689629/how-to-extract-get-variable-from-query-string-in-codeigniter) – Pradeep Jun 06 '18 at 14:08
  • see also : https://stackoverflow.com/questions/34694848/how-to-change-url-of-get-method-form-to-slashes-in-codeigniter-3 – Pradeep Jun 06 '18 at 14:08

2 Answers2

1

You can use get method and get url data like this $this->input->get();

If you want to make the URL clean so you should route your url

go to

application/config/routes.php

$route['search/(:any)'] = "controller/function/$1";

You can get url segment in controller use this $this->uri->segment(2);

F5 Buddy
  • 474
  • 4
  • 4
0

Try this codes:

Html:

<?=form_open(base_url(), array('id' => 'search'))?>
    <input type="text" class="form-control" placeholder="Keyword..." name="query" id="search_query">
    <button class="btn btn-default" id="btn-search" type="button">
        Search
    </button>
<?=form_close()?>

Javascript:

$("#btn-search").click(function(){
    var query = $("#search_query").val();
    window.location = '/search/'+query;
});

$('form#search').bind("keypress", function(e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        var query = $("#search_query").val();
        return window.location = '/search/'+query;
    }
});

Route:

$route['search/(:any)'] = 'Controller/search/$1';

Controller:

public function search($keyword = null)
{
    if($keyword == null)
        redirect(base_url());
    else
    {
        $keyword = urldecode($keyword);
        $data = array(
            'category' => $this->Category_Model->search($keyword)
        );
        $this->load->view('search', $data);
    }
}
Onur KAYA
  • 234
  • 1
  • 9