提交表单时,从URL中移除%5B%5D [英] Remove %5B%5D from URL when submitting form

查看:212
本文介绍了提交表单时,从URL中移除%5B%5D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用多个同名的复选框提交表单时,我得到的URL如下所示:
www.mysite.com/search.php?myvalue%5B%5D=value1&myvalue% 5B%5D = value2

When I submit a form with multiple checkboxes that have the same name I get a URL that looks something like this: www.mysite.com/search.php?myvalue%5B%5D=value1&myvalue%5B%5D=value2

有没有办法可以移除%5B%5D来使网址变得很漂亮,如htaccess?

Is there someway that I can remove the %5B%5D to make the URL "pretty", with something like htaccess?

代码:

<form>
     <input type="checkbox" name="myvalue[]" value="value1">
     <input type="checkbox" name="myvalue[]" value="value2">
</form>


推荐答案


有些地方我可以移除%5B%5D来使URL漂亮,像htaccess?

无。 [] 在网址中为保留字符,所以他们肯定需要 URL编码

No. The [] are reserved characters in URLs, so they definitely need to be URL-encoded.

如果使用POST不是一个选项,因为它是一个搜索表单,所以最好的办法就是给它们分别指定一个值为1左右的不同名称。

If using POST is not an option, which makes sense given that it's a search form, your best bet is to just give them each a different name with a value of 1 or so.

<form>
    <input type="checkbox" name="option1" value="1" />
    <input type="checkbox" name="option2" value="1" />
</form>

或者,如果您确实坚持使用相同的名称,那么您应该提取查询字符串而不是在名称中使用 [] 后缀获取参数时依赖于返回数组的PHP特定功能。

Or, if you really insist in them having the same name, then you should be extracting the query string yourself instead of relying on the PHP specific feature of returning an array when obtaining a parameter with a [] suffix in the name.

$params = explode('&', $_SERVER['QUERY_STRING']);

foreach ($params as $param) {
    $name_value = explode('=', $param);
    $name = $name_value[0];
    $value = $name_value[1];
    // ... Collect them yourself.
}

通过这种方式,您可以继续使用无支撑的名称。

This way you can just keep using the braceless name.

<form>
    <input type="checkbox" name="option" value="option1" />
    <input type="checkbox" name="option" value="option2" />
</form>

这篇关于提交表单时,从URL中移除%5B%5D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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