douqianrou9079 2013-01-20 21:47
浏览 34
已采纳

搜索表单发布数据和平面URL

I have a form which filters articles based on tags. So a user may visit example.com/news submit the tags to filter by (e.g. tag1, tag2) using post data, this reloads the page with the filtered articles but the same URL.

The following url will bring back the same articles: example.com/news/tag1+tag2

Both methods go through the same controller. I would like to make users who have filtered by tags using the form are redirected to example.com/news/tag1+tag2 url format.

What is the best way of doing this? Would it be to send all tag filter requests through a search controller and then create a redirect to example.com/news/tag1+tag2?

  • 写回答

2条回答 默认 最新

  • ds3016 2013-01-20 21:52
    关注

    Seems like you should not do any search based on the initial submission of the filtered tags. If you passed through the search controller once then redirected, you would end up doing two searches.

    If a user submits tags to filter, use those only to build a URL and redirect directly to the URL containing the filtered tags. Since you said it goes to the same search controller, that will subsequently initiate the correct search only once and the user's URL will already be what you want its end result to be.

    So just retrieve the filtered tags from $_POST and immediately redirect to the end result URL which triggers the correct search.

    Pseudo PHP

    $valid_tags = array_filter($_POST['tags'], function($t) {
       // validate tags as alphanumeric (substitute the appropriate regex for your tag format)
       // this discards non-matching invalid tags.
       return preg_match('/^[a-z0-9]+$/i', $t);
    });
    // Don't forget to validate these tags in the search controller!
    // Implode the tags (assuming they are received as an array) as a space separated string
    // and urlencode() it
    $tags = urlencode(implode(" ", $valid_tags));
    header("Location: http://example.com/news/$tags");
    exit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程