I have a form with multi-select field:
<select <?php echo esc_attr($item['pgggo_grid_sort_and_filter_multiselect']); ?> name="pgggo-taxon-select[]" class="ui fluid dropdown">
a foreach loop generates this select field but once the form is submitted, the query is a normal variable instead of generating array variables. It generates the query this way:
?pgggo-taxon-select%5B%5D=14&pgggo-taxon-select%5B%5D=1
Please suggest a solution to have it in array format.
I have a form with multi-select field:
<select <?php echo esc_attr($item['pgggo_grid_sort_and_filter_multiselect']); ?> name="pgggo-taxon-select[]" class="ui fluid dropdown">
a foreach loop generates this select field but once the form is submitted, the query is a normal variable instead of generating array variables. It generates the query this way:
?pgggo-taxon-select%5B%5D=14&pgggo-taxon-select%5B%5D=1
Please suggest a solution to have it in array format.
Share Improve this question edited Nov 8, 2019 at 8:31 meDeepakJain 948 bronze badges asked Nov 5, 2019 at 12:04 user145078user145078 5 |1 Answer
Reset to default 0 +50Why 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/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:
HTTP Methods GET vs POST
When should I use GET or POST method? What's the difference between them?
method
is likelyGET
, so the[
and]
are URL-encoded. But PHP would still treat the input ($_GET['pgggo-taxon-select']
) as an array. – Sally CJ Commented Nov 5, 2019 at 12:34get_query_var
..get seems to be the common method which adds the query string to the URL ..but post method does not add any query string but I still the required array. Can I use a post ? is it recommended? – user145078 Commented Nov 5, 2019 at 16:49get_query_var()
, it is not a replacement for retrieving submitted data with$_POST
or$_GET
. More on this in the docs. – Cas Dekkers Commented Nov 8, 2019 at 14:58