jQuery.post(),PHP和重定向 [英] jQuery.post(), PHP and redirects

查看:81
本文介绍了jQuery.post(),PHP和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题现在解决了。

您好,

我想解决问题我可能已经从错误的方向接近,所以也许有人可以建议一个更好的解决方案。

I am trying to solve a problem that I may have approached from the wrong direction, so maybe someone could suggest a better solution.

我写一个小的web应用程序的工作,有一个搜索功能。当用户提交搜索字符串时,表单被发布到CodeIgniter控制器。如果搜索返回结果,CodeIgniter加载包含结果的页面。如果没有结果,用户的当前页面只是重新加载,没有任何迹象表明搜索没有返回任何结果。所以,这不是用户友好的,我决定重新做一些帮助从 $。ajax()

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().

这是相当容易,但现在我被困在试图找出如何处理我收到的数据,如果搜索成功。
我知道 $。load()可以替换页面的一部分,但我需要重定向用户到一个完全不同的页面,而POSTing数据
我的ajax搜索刚刚返回。从PHP返回的数据是 json_encode()格式。

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.

注意: $。jGrowl()是一个jQuery插件,

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

我接受从 $。post()返回的数据。并使用 $。load()将其发回到同一个控制器,反过来获取数据并加载正确的视图文件。唯一的缺点是URL仍然是相同的,打破了几件小东西,但我可以处理这...

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:我终于使它像我想要的那样工作。我做的是,把从PHP返回的JSON格式的数据,发送回同一个控制器,让CodeIgniter负责加载视图,这种方式我所有的URL依赖的东西也是。

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();

但是有两个警告。


  1. appendTo($(body))

POST在第一个空格后截断了我的JSON字符串,所以我不得不使用 str_replace

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));

echo str_replace(' ', '%20', json_encode($search_results));

此问题现已解决。

推荐答案

这已经解决了。解决方案在我的原始帖子。

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

这篇关于jQuery.post(),PHP和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆