Remove %5B%5D from URL when submitting form
Solution 1
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.
<form>
<input type="checkbox" name="option1" value="1" />
<input type="checkbox" name="option2" value="1" />
</form>
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>
Solution 2
[
and ]
are reserved characters in a URL, so the browser must encode them in order for the URL to work correctly. You cannot have these characters in a URL. Nor can you have any other reserved characters such as spaces, ampersands, etc. They will all be encoded automatically for you (in many cases, even if you type the URL into the browser manually).
If you need a "pretty URL" you can:
Not use a form at all; provide a link to a known "pretty" URL.
Accept the ugly URL, but redirect it immediately to the pretty URL in point 1 above.
Avoid using angle brackets at all in your field names (but this would mean a lot of changes to your back-end code too)
Use a
POST
method on the form, so that the field data doesn't show up on the URL at all (but this would mean you don't have a link the user can bookmark).
If you must "prettify" this URL, my suggestion would be option 2 above.
Frankly, though, I wouldn't worry about it. People get waaaay to stressed about "pretty" URLs. I don't really get why.
- Very few people I know ever actually type in a URL longer than just a domain name.
- If you're worried about SEO for this, don't -- the search engine bots know what ULR encoding is and can look past it.
- The only other reason for wanting a "pretty" URL is so that it looks good if users share it via an email link or something. To be honest, if you're worried about URL prettyness for that and it's got form fields in it then it's already too ugly, with just the
&
and=
signs all over the place. The encoded brackets really don't make it any worse.
So my honest answer is: don't sweat it. It's normal; ignore it; get on with more important parts of your web development work.
Oskar Persson
Know some code here and there. Python, PHP, MySQL, Lua, Java, HTML, CSS, Javascript.
Updated on July 30, 2022Comments
-
Oskar Persson almost 2 years
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
Is there someway that I can remove the %5B%5D to make the URL "pretty", with something like htaccess?
Code:
<form> <input type="checkbox" name="myvalue[]" value="value1"> <input type="checkbox" name="myvalue[]" value="value2"> </form>
-
Jakob Weisblat over 11 years"No. The [] are reserved characters in URLs, so they definitely need to be URL-encoded." - BalusC above.
-
Oskar Persson over 11 yearsBut if I write the URL manually and puts
[]
instead of%5B%5D
it still works. -
BalusC over 11 yearsOn manually entering the URL, it's the webbrowser itself who hides that away. By the way, if you were using JSP/Servlet instead of PHP, you could just use
name="myvalue"
in combination withrequest.getParameterValues("myvalue")
. See also among others: stackoverflow.com/questions/1928675/… The[]
being mandatory in PHP is really a PHP specific "quirk". -
Oskar Persson over 11 yearsThanks for your answer and opinion, though I have some answers and questions regarding it. 1. I need to use a form since it's for a searching filter. 2. Should I make a script in php or something that translates
%5B%5D
to[]
? 3. Need 'em. 4. Have to use a GET method since it's a search form. -
Can O' Spam almost 9 years@Oskar, you do know you dont have to use GET, GET and POST both go to
$_REQUEST
...