2

I have a URL passing values from form checkboxes to a template. All the checkboxes are grouped with the same name= attribute. When multiple checkboxes are selected, the query string looks something like:

?checkValue=12345&checkValue=23456&checkValue=34567

However, craft.request.getQuery('checkValue') returns only the last value (34567 in this case).

How can I retrieve all the values so they can be used and manipulated?

EDIT: I've corrected the naming of the checkboxes from name="checkValue" to name="checkValue[]", so now my query string looks like this:

?checkValue%5B%5D=12345&checkValue%5B%5D=23456&checkValue%5B%5D=34567

Does that change what can be accessed using craft.request.getQuery('checkValue')?

3

This question is not really Craft related. Since you just don't send a valid request. You can't send one key multiple times unless you do it in an array.

The php $_GET is a simple array with all your values and you overwrite them again and again.

https://stackoverflow.com/questions/5098397/how-to-pass-an-array-in-get-in-php

| improve this answer | |
  • Please see edits to original post. – Philip Jones Dec 14 '17 at 0:32
2

Using the corrected checkbox name name=checkValue[] the submitted values

?checkValue%5B%5D=12345&checkValue%5B%5D=23456&checkValue%5B%5D=34567

are converted to an array. The array values can now be accessed using

{% set checkVals = craft.request.getQuery('checkValue') %}

{{ checkVals[1] }} {# outputs 23456 #}

or

{% for cVal in checkVals %}
    {{ cVal }}
{% endfor %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.