Search code examples
arraysperlpostmojolicious

How to send an array as a post variable


I'm using Perl and a Perl framework (not sure if that's important). What I'm trying to do is create an arbitrary amount of inputs in a form (so a user would select as many things as they want from a list) then send that as part of an HTTP post. Is there even a way to do that? If so, how would I retrieve that info in Perl?


Solution

  • If I understand correctly you want to do something like this:

    <form action="/test" method="post">
        <input type="text" name="elements[]" value="">
        <input type="text" name="elements[]" value="">
        <input type="text" name="elements[]" value="">
        <input type="text" name="elements[]" value="">
        <input type="text" name="elements[]" value="">
    
        <input type="submit" value="Submit">
    </form>
    

    Then when you submit the form you will get an array "elements[]" (or it could be only "elements" but with "[]" is more clear), that will have all the fields.

    If you use checkboxes, you will get only the ones that are selected.

    Or you could make a selectbox with "multiple".