I am new to web programming and this is my first SO question, so hopefully I am asking the right way. I am building a simple survey-maker (similar to google forms or survey monkey) and I have a web form that allows the user to add options to a question. Each option has two fields, ‘label’ and ‘value’. The form looks like this:
Option One
------------------ ------------------
Label: | | Value: | |
------------------ ------------------
Option Two
------------------ ------------------
Label: | | Value: | |
------------------ ------------------
...
...
... // user can "add option" and additional "rows" will appear in the form
[ SUBMIT ]
I have used a basic web form like this:
<form class="well form" action="/submitform" method="post">
<p><input type="text" name="label_1">
<input type="text" name="value_1"></p>
<p><input type="text" name="label_2">
<input type="text" name="value_2"></br></p>
<p>... // and so forth, to support n sets of two fields
...
...
<button type="submit">Submit</button>
</form>
I successfully get request.form and can access each variable in the dict. But of course it’s a single flat dict:
[{'label_1': 'labelone', 'value1': 'valueone', 'label_2': 'label two'}]
and so forth. I want a list of dicts like this:
[ {'label': 'labelone', 'value': 'valueone'}, {'label': 'labeltwo', 'value': 'valuetwo'}]
I’ve thought of several hackish ways to do this, but all require foreknowledge of the number and type of form options. I’d rather find a more flexible solution.
Any best practice here? A lot of sites do things like this so I’m guessing there’s a best practice that I’m missing.
What about this?