I am working on an application. I got URL & result http://localhost/mydss/result/search
When I query with the keyword "radha", getting a result works fine.
However, when I type URL http://localhost/mydss/result/search/radha I do not get any result.
How should parameters be passed to the URL?
home.php
<form action="<?php echo site_url('result/search');?>" method = "post" name="search" id="search">
<section class="s_form">
<input type="search" placeholder="Search..." id="search" name="search" autocomplete="off" value="">
<button type="submit" class="btn search_button gray"><span class="i_search_g"> </span></button></form>
Result.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Result extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('result_model','',TRUE);
}
function search()
{
$keyword = $this->input->get_post('search', TRUE);
$data['keyword'] = $keyword;
$data['results'] = $this->result_model->search($keyword);
$this->load->view('result',$data);
}}
You would get your answer like this;
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Result extends CI_Controller {
public function index() {
}
public function search($query="radha"){
}
}
But this is not great for a search query as CodeIgniter will restrict what you can search for in the query string.
It's better for searching query's like this;
http://localhost/mydss/result/search?q=radha
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Result extends CI_Controller {
public function index() {
}
public function search(){
$query = $this->input->get("q");
}
}
For get this type of url you need to add jquery code on home.php
# add here jquery file...
<script>
$(document).ready(function(){
$("form").submit(function(){
var action = $('#search').val() ? $('#search').val() : "";
$("#search-form").attr("action", "<?php echo base_url();?>result/search/" + action);
});
});
</script>
And slightly change in your form tag. Chang id search
to search-form
.
<form action="" method = "post" name="search" id="search-form">
When you click on submit button then it takes the value of input field
and puts in form action.
And finally on Controller. You can call like this -
public function search($keyword = null)
{
$data['keyword'] = $keyword;
$data['results'] = $this->result_model->search($keyword);
$this->load->view('result',$data);
}