0

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.

5
  • 2
    The form's method is likely GET, so the [ and ] are URL-encoded. But PHP would still treat the input ($_GET['pgggo-taxon-select']) as an array.
    – Sally CJ
    Nov 5, 2019 at 12:34
  • @SallyCJ Thhhhhhhhhhhhank You!!!!!!. I was so confused with the URL structure and it's been more than a week! Please make it as an answer. Both get and post method word with get_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
    Nov 5, 2019 at 16:49
  • Be careful when using get_query_var(), it is not a replacement for retrieving submitted data with $_POST or $_GET. More on this in the docs. Nov 8, 2019 at 14:58
  • @CasDekkers hmm if we add "public query variables that are recognized by WP_Query" then it will be all good right?
    – user145078
    Nov 8, 2019 at 15:01
  • Sure, if that's what you want! Nov 8, 2019 at 15:29

1 Answer 1

0
+50

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:

  1. The form's method is post (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">

  2. Or (most likely in your case), the form's method is get (which corresponds to the HTTP GET method, and get is the default form method).

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.