0

when submitting a form via POST, one common way to send arrays is to give names as follows:

<input name="foo[]" value="" />
<input name="foo[]" value="" />
<input name="foo[]" value="" />

However, when sending this via method GET, the browser urlencodes the [], which results in a url like ?foo%5B%5D=bar instead of ?foo[]=bar.

Can this be prevented?

Gundon
  • 2,081
  • 6
  • 28
  • 46
  • do you really need $_GET? I mean $_POST is way better, in terms of security, cleansyness etc.. – treyBake Sep 11 '18 at 15:34
  • if you have access to a backend language (e.g. php) you can use `implode('|', $foo)` and it will output `?foo=val1|val2|val3` – treyBake Sep 11 '18 at 15:35
  • The form in question sends filter-params for a search. The resulting URL should be shareable. Also, I am using Symfony and try to only use what they already implemented (i.e don't mess with the $_GET-var myself) – Gundon Sep 11 '18 at 15:35
  • There's nothing wrong with that URL. It is shareable as is. – dustytrash Sep 11 '18 at 15:36
  • 1
    wait - if it's a search - why is foo an array? surely you're searching by unique params rather than multiple ones that fits one param? – treyBake Sep 11 '18 at 15:37
  • Nope. If I POST a search the user is not able to simply copy and paste the url to another user who will then do the same search. Anyway, that is not the question. The question is strictly about GET – Gundon Sep 11 '18 at 15:37
  • @Pete This is not a duplicate. This question is about the key, not the values and this question is _not_ about REST-APIs – Gundon Sep 11 '18 at 15:42
  • @gundon it's not possible - the standard way browsers will treat input names is to urlencode them. The answers in the dupllicates all say it is not possible to stop the form from url encoding - that applies to both the names and the values. I would think about using a comma seperated list if it needs to be an array, otherwise as this guy says, why would you use an array for searching, surely each search parameter should have it's own name – Pete Sep 11 '18 at 16:00
  • Also the last answer about php would suggest that it doesn't matter that the names are urlencoded as php can read them properly (if that is the server side language you are using) – Pete Sep 11 '18 at 16:07

0 Answers0