Why the ?pgggo-taxon-select%5B%5D=14&pgggo-taxon-select%5B%5D=1
in the URL
There are three common reasons why would one see that upon submitting the form:
-
The form’s
method
ispost
(which corresponds to the HTTP POST method) and either:a) The current page indeed has that string in the URL. E.g. You’re on
example.com/my-page/?pgggo-taxon-select%5B%5D=14&pgggo-taxon-select%5B%5D=1
b) The form’s
action
has that string. E.g.<form action="/my-app?pgggo-taxon-select%5B%5D=14&pgggo-taxon-select%5B%5D=1">
-
Or (most likely in your case), the form’s
method
isget
(which corresponds to the HTTP GET method, andget
is the default formmethod
).
Okay, but why not ?pgggo-taxon-select[]=14&pgggo-taxon-select[]=1
?
Because special characters like square brackets ([
and ]
) (that have specific meaning) in URLs need to be percent-encoded/URL-encoded (more details on MDN or Wikipedia); hence, those brackets become %5B
and %5D
respectively in the URL — try urlencode( '[' )
in PHP.
So don’t worry.. all is good.
PHP would still treat the input ($_GET['pgggo-taxon-select']
) — when sent as pgggo-taxon-select%5B%5D
via the GET method — as an array. In fact, PHP (or the server) may not be able to properly parse the input if it was using the unescaped [
and ]
.
From your comment (formatted for brevity), “but post
method does not add any query string but I still get the required array. Can I use a post
? Is it recommended?“:
Just use the method that better fits the form’s context/purposes — if it’s for uploading a file, you should use post
; if it’s just to read/display posts filtered by certain criteria, you can just use get
.
And these resources would help you understand when to use what HTTP method in submitting a form: