-3

Let's say I have this simple form:

<form action="/search/" method="get" role="search">
    <input name="cars[]" value="saab">
    <input name="cars[]" value="honda">
    <input name="cars[]" value="toyota">
    <input type="submit" value="submit">
</form>

After clicking submit I am being redirected to the search results page and the URL looks like this: https://example.com/search/?cars=saab%5B%5D&cars=honda%5B%5D&cars=toyota

Questions:

  1. Why is this piece of code %5B%5D added to the url and can I get rid of it?
  2. Why is cars appearing multiple times? Can I simple have ?cars=saab,honda,toyota?

Thanks

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
odedta
  • 2,430
  • 4
  • 24
  • 51
  • the `%5B%5D` are the urlencoded characters `[]`. Are you sure that is the exact URL you get? Let me test. – KIKO Software Dec 26 '22 at 21:20
  • Ah, no the URL is your question isn't actually what you get. Please check it more carefully. The URL I see is: `test.html?cars[]=saab&cars[]=honda&cars[]=toyota`. – KIKO Software Dec 26 '22 at 21:22
  • you should remove the brackets from the variable name - i.e. `[]` - that's the variable name, not the type. – blurfus Dec 26 '22 at 21:22
  • Hey, removing the `[]` from the input name indeed removed that `%5B%5D` urlencoded part, however, now php `$_GET` is only showing the first variable and the variables in the query parameter appear without the `[]` part. This still doesn't solve the multiple query parameters with the same name though. Thanks for the help!!! – odedta Dec 26 '22 at 21:26
  • Ah, on the PHP side, the recommended way is to add the brackets to the name. See: https://stackoverflow.com/a/9547490/600486 - keep in mind that this seems limited to PHP (i.e. using brackets in variable names to auto-converted into arrays in the backend) – blurfus Dec 26 '22 at 21:32
  • You've added PHP to your tags, but there's nothing relating to PHP in your question? My guess is that that's where the problems start. – KIKO Software Dec 26 '22 at 21:33
  • @KIKOSoftware As it happens, I am using PHP to pull the query string data but this have nothing to do with the behavior I talked about, try to create to HTML files and see for yourself. – odedta Dec 27 '22 at 02:31

1 Answers1

-2

Each input must have a unique name, just change names.

  • 1
    This is not true if you plan on sending multipe values (i.e. an array of values) for the same variable name – blurfus Dec 26 '22 at 21:21