Remove [] from URL when submitting form

前端 未结 2 1701
爱一瞬间的悲伤
爱一瞬间的悲伤 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:48

    [ 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:

    1. Not use a form at all; provide a link to a known "pretty" URL.

    2. Accept the ugly URL, but redirect it immediately to the pretty URL in point 1 above.

    3. Avoid using angle brackets at all in your field names (but this would mean a lot of changes to your back-end code too)

    4. 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.

提交回复
热议问题