Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove %5B%5D from URL when submitting form

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>
like image 775
Oskar Persson Avatar asked Jan 04 '13 16:01

Oskar Persson


2 Answers

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>
like image 182
BalusC Avatar answered Sep 25 '22 17:09

BalusC


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

like image 25
SDC Avatar answered Sep 22 '22 17:09

SDC