Background Information
I have a php web application that uses a template approach to my pages. So in other words, I have a header.php, footer.php and a leftnav.php. Each view in my php app includes all three pages listed above. So far, so good. Everything works.
As a part of my leftnav.php, I have the following code:
<div class="input-group custom-search-form">
<input type="text" class="form-control" placeholder="Quick Search..." id="searchtext">
<span class="input-group-btn">
<button class="btn btn-default" type="button" id="qsearch">
<i class="fa fa-search"></i>
</button>
</span>
</div>
The idea is to provide the users with a "quick search" option that they can use where ever they are in the application... It always shows up because every page includes the same menu stored in leftnav.php.
Problem
This code works fine ONLY if you launch my app and trigger the search right away. But if you navigate away from the default controller, and then the user tries to run the search... nothing happens because they're not in the default controller.
So for example, here's the default URL when you launch my web app:
http://myserver/testapplication/index.php
From here, you can run the quick search and the dashboard controller kicks in.
But from somewhere else like this:
http://myserver/testapplication/index.php/widgets/addwidget
the search button does nothing.
Question
I don't know how to set up my code so that whenever the user clicks on the search submit button, the first thing that happens in the system redirects to the dashboard controller.
The logic that actually runs the search is in my default controller, called "Dashboard". The controller looks like this:
public function index()
{
$data['main_content'] = "dashboard";
$this->load->view('includes/template',$data);
}
public function quicksearch()
{
//grab search text
$seg3 = $this->uri->segment(3); //hardwaremodel
$searchresults = $this->dashboard_model->quick_search($seg3);
$retval = array();
foreach ( $searchresults as $id => $value )
{
//logic to build results array
array_push($retval, $temp);
}
echo json_encode($retval);
}
}
And the view that includes the logic to display the search results is in the "dashboard.php" file.
$(document).ready(function(){
$(".searchresults").hide();
$('#qsearch').click(function(){
$('#qsearchresults tbody').empty();
var searchstring = $('#searchtext').val();
var searchurl = "<?php echo site_url('dashboard/quicksearch/');?>" + searchstring;
$.ajax({
url:searchurl,
type:'POST',
dataType:'json',
success: function(res) {
if (res.length > 0) {
var htmlstring = "<tr><th>ID</th><th>PH</th><th>Site</th><th>Location</th><th>Department</th><th>Description</th><th>Fax?</th><th>Last Assigned Date</th><th>Score</th></tr>";
for (var key in res) {
tmpurl = "<?php echo site_url('did/getdidbyobjid/')?>";
//bunch of logic to build html string with search results
}
$('#qsearchresults tbody').append(htmlstring);
$('.searchresults').show();
}
},
error: function(xhr, req, err) {
var err = eval("(" + xhr.responseText + ")");
console.log(err.Message);
}
});
});
});
EDIT 1
I tried to put all the javascript that handles the search button click event into a separate js file called qsearch.js. qsearch.js is now included in the header.php... which in turn in included by all views.
<head>
<script src="<?php echo base_url(); ?>assets/l/js/qsearch.js"></script>
</head>
Test
This works: (because it's launch the default controller Dashboard)
http://myserver/myapp/index.php/
But let's say i go to:
http://myserver/myapp/index.php/reports/dept
the search IS WORKING... but it has no where to display the results. The javascript that handles the search click event does this in part:
$('#qsearchresults tbody').append(htmlstring);
"htmlstring" contains the results of the search... but not all pages have the html table called qsearchresults.
So I guess I need to know if it's possible to redirect the user to the dashboard controller and then run the search.
If not, no worries. I will redo the code and use a static form inside the leftnav.php menu. But I'm so close, I'd like to leave it as is... if possible
I found this link: How to redirect on another page and pass parameter in url from table? and I'm trying to see if I can adapt it to my use case.