THIS PROBLEM IS NOW SOLVED. THANK YOU TO EVERYONE WHO REPLIED.
Hello,
I am trying to solve a problem that I may have approached from the wrong direction, so maybe someone could suggest a better solution.
I am writing a little web app for work, that has a search function. When user submits a search string, form gets posted to a CodeIgniter controller. If search returns a result, CodeIgniter loads a page with results. If there is no results, user's current page just gets reloaded, without any indication that search returned no results. So, this is not user friendly at all and i decided to re-do that with some help from $.ajax()
.
That was rather easy, but now I am stuck trying to figure out what to do with the data i receive back, if search is successful.
I know $.load()
can replace a part of the page, but i need to redirect the user to a completely different page, while POSTing the data
my ajax search just returned. The data that comes back from PHP is in json_encode()
format.
NOTE: $.jGrowl()
is a jQuery plugin that displays a Growl-like message.
$("#alias_s").submit(function() {
event.preventDefault();
var term = $('input#alias_search').val();
$.post("<?=base_url()?>aliases/searchAliases", {
searchTerm: term },
function(data) {
if(!data) {
$.jGrowl("Search for '" + term + "' returned no results", { life: 5000 });;
} else {
//
// How do i post this data to another page and redirect a user there?
//
}
});
UPDATE: I managed to find a work-around to this issue.
I take the data that was returned from $.post()
and use $.load()
to POST it back to the same controller, that in turn takes the data and loads correct view files. The only downside is that URL it still the same, which breaks couple of small things, but I can deal with that...
$('#content').load("<?=base_url()?>aliases", { searchData: data});
UPDATE #2: I finally made it work like i wanted. What i did, is take the data that was returned from PHP in JSON format, post it back to the same controller, and let CodeIgniter take care of loading views, this way all my URL dependent stuff works too.
$('<form action="CONTROLLER" method="post"><input type="hidden" id="searchData" name="searchData" value='+data+'></form>').appendTo($("body")).submit();
There were 2 caveats though.
appendTo($("body"))
was needed to make it the form submit correctly in Firefox.POST was cutting off my JSON string after first space, so i had to use
str_replace()
in php do fix that.echo str_replace(' ', '%20', json_encode($search_results));
THIS PROBLEM IS NOW SOLVED. THANK YOU TO EVERYONE WHO REPLIED.