2

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?

3
  • I'm not quite following. Is the question how to generate a dynamic form? Commented Aug 18, 2014 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) Commented Aug 19, 2014 at 12:12
  • Possible duplicate of Serialize complex form to JSON object using jQuery Commented Jul 18, 2017 at 20:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.