Remove [] from URL when submitting form

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

    <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>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题