weixin_33728268 2010-07-01 18:59 采纳率: 0%
浏览 22

jQuery.post(),PHP和重定向

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.

  • 写回答

4条回答 默认 最新

  • weixin_33728708 2010-07-01 19:13
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp