views:

76

answers:

4

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.

  1. appendTo($("body")) was needed to make it the form submit correctly in Firefox.

  2. 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.

+1  A: 

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.

I think you should just fix that bad behavior, and have the "Search" action return the page with some error information. Using AJAX doesn't really do you any good if you want to completely reload the page anyway.

Pointy
Agreed... just use a form if you want to redirect
bschaeffer
The thing is that i do not want to redirect them to a page just to say that no results were found, and all pages across the app are completely different, so there is no uniform location on them to show a message, so for the sake of consistency i figured i would use something like jGrowl plugin, since i already use it for couple of other things
solefald
Well the problem is that you can *either* do an AJAX-style request *or* a regular "POST" request; you can't tell the browser that you changed your mind halfway through. If you don't want a "Search failure" page, then you'll have to find a way to drop the search results into the original page.
Pointy
Thanks Pointy, i was just thinking of doing that. I guess i will just have to replace data in main container div. I really should have thought of that beforehand, now i need to change a bunch of stuff to work with this approach.
solefald
A: 

If you want redirect the user another page, why do you stop the execution a form in a javascript?

turbod
You mean `event.preventDefault();` ?Because without it, the function does not work in Chrome as the event over-writes the function and i end up with null result returned from the function
solefald
A: 

Maybe this work, but i not tested.

$("#alias_s").submit(function() {


    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 });;
               return false;
             } else {
               //
               //  
               //
             }
});
turbod
I think you misunderstood by question. `preventDefault()` has nothing do do with my problem, as the matter of fact, it makes my function work. I was having issues trying to figure out what to do with the data, but just found a solution that works great.
solefald
A: 

This has been solved. Solution is in my original post.

solefald