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
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
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);
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);
}
}