jQuery.post(), PHP and redirects

痞子三分冷 提交于 2019-12-25 01:26:56

问题


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:


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.




回答2:


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




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/3160840/jquery-post-php-and-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!