Remove [] from URL when submitting form

前端 未结 2 1715
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 05:22

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%

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 05:31

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

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

    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.

    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.

提交回复
热议问题