最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Form element name - array type is not working

programmeradmin1浏览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.

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
  • 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 Commented 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 Commented 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. – Cas Dekkers Commented 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 Commented Nov 8, 2019 at 15:01
  • Sure, if that's what you want! – Cas Dekkers Commented Nov 8, 2019 at 15:29
Add a comment  | 

1 Answer 1

Reset to default 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/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:

  • HTTP Methods GET vs POST

  • When should I use GET or POST method? What's the difference between them?

发布评论

评论列表(0)

  1. 暂无评论