doupu0619 2018-10-21 19:32
浏览 466

在$ _GET查询网址中使用'+'代替%2C'

I have a form with a location search input. The search input uses google places autocomplete. I am passing the value of the search input to a hidden field named 'query'. When the form is submitted the value of the hidden field is then appended to the URL as a querystring:

<input id="foo" type="text" name="search" placeholder="Search...">
<input id="bar" type="hidden" name="query" value="">

From the above example, the URL will look like this:

http://example.com/?query=ExampleC%2Blocation%2C%2BUS

This querystring is only used for analysing analytics. Is there a way to format the URL in a more human-readable way with pluses? So, for example:

http://example.com/?query=Example+location,+us

In my Javascript file I have tried the following:

document.getElementById("bar").value = usersearch.split(' ').join('+');

And in my PHP I have added the following for already-performed searches:

<input id="bar" type="hidden" name="query" value="<?php echo urlencode($_GET['query']) ?>">

Neither seems to work. The URL still ends up with %2B etc.

  • 写回答

2条回答 默认 最新

  • dongqiang4819 2018-10-21 19:40
    关注

    JavaScript: decodeURIComponent

    const someEncodedUri = 'http://example.com/?query=ExampleC%2Blocation%2C%2BUS';
    console.log(decodeURIComponent(someEncodedUri)); // http://example.com/?query=Example+location,+us
    

    PHP: urldecode

    $someEncodedUri = 'http://example.com/?query=ExampleC%2Blocation%2C%2BUS';
    echo urldecode($someEncodedUri));
    

    Do note that if you are actually hitting the URL, you will want to hit it encoded to avoid problems, and then just decode it on the server for analytics purposes.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测