1

I have a form, part of which is dynamic (using jinja templates). For example, when it renders, the number of fruits is unknown. Let's say I have the below form inputs:

<input type="text" name="{{ fruit1 }} name" value="apple">
<input type="text" name="{{ fruit1 }} size" value="5">
<input type="text" name="{{ fruit1 }} color" value="red">

<input type="text" name="{{ fruit2 }} name" value="banana">
<input type="text" name="{{ fruit2 }} size" value="7">
<input type="text" name="{{ fruit2 }} color" value="yellow">

I'd like to be able to post to Flask via AJAX the form input in the following JSON structure:

{
    "fruit": [{
        "id": "fruit1"
        "name": "apple",
        "size": "5",
        "color": "red"
    }, {
        "id": "fruit2"
        "name": "banana",
        "size": "7",
        "color": "yellow"
    }]
}

Outside of manually shaping the data using javascript, is there a more elegant way to do this--perhaps using nested inputs in some manner?

ChrisArmstrong
  • 2,491
  • 8
  • 37
  • 60
  • I'm not quite following. Is the question how to generate a dynamic form? – Rachel Sanders Aug 18 '14 at 23:37
  • No, it is whether there is a way to arrange the form inputs so that the user input can more easily be translated into a nested JSON object as shown above. If not, is there maybe a simple Flask or JS method to this (not as familiar with Javascript/jQuery) – ChrisArmstrong Aug 19 '14 at 12:12
  • Possible duplicate of [Serialize complex form to JSON object using jQuery](https://stackoverflow.com/questions/3672995/serialize-complex-form-to-json-object-using-jquery) – j boschiero Jul 18 '17 at 20:52

0 Answers0