Wapp

Handle multiple values per form input name?
Login

Handle multiple values per form input name?

(1) By anonymous on 2020-03-21 18:43:37 [source]

Hello,

I've created a form wherein multiple checkboxes all use the same name attribute but can't seem to retrieve the values.

As an example, if I were to check off each of the units below

<label for="unit_1"><input type="checkbox" id="unit_1" name="unit" value="1" >each</label>
<label for="unit_2"><input type="checkbox" id="unit_2" name="unit" value="2" >bag</label>
<label for="unit_3"><input type="checkbox" id="unit_3" name="unit" value="3" >crate</label>

I'd expect the following form data to be submitted:

unit=1&unit=2&unit=3

I do see that my browser has made the request with these parameters, but I'm unsure of how to retrieve each of these values in Wapp. I only seem to be able to retrieve the last set value, i.e. "3".

Does Wapp support this type of parameter input?

(2) By D. Richard Hipp (drh) on 2020-03-21 19:32:53 [link] [source] in reply to 1

When Wapp is parsing query parameters (or POST parameters) it reads from left to right and set its parameter variables for each query parameter it encounters. If the query string is unit=1&unit=2&unit=3 then it will successively set unit to 1, to 2, and finally to 3. But it only has a single "unit" variable. In this way, the query parameter processing is kept simple.

The easiest solution is to give each checkbox a different name. The other option is to parse of the value of the QUERY_STRING (or CONTENT if you are using POST) yourself. But that is complex and error-prone.

Why not just give each checkbox a distinct name?

(3) By anonymous on 2020-03-21 20:43:51 [link] [source] in reply to 2

Thanks for the reply. Giving each checkbox a distinct name is definitely a legitimate approach. I know some frameworks allow for the previously described functionality and wanted to be sure I hadn't overlooked this feature in Wapp, if it was indeed available.

It is easy enough to give each input a unique id, e.g. unit_1, unit_2, unit_3 and then retrieve them via the wapp-param-list command:

wapp-param-list unit*

P.S. Can the documentation be updated to point out that the NAME arg in the wapp-param-list command supports a GLOB pattern? It's present in the code, which is how I figured it out, but it may be helpful to others if its presented on the https://wapp.tcl.tk/home/doc/trunk/docs/commands.md page itself.