multiple instances of a single form on one page

116 views
Skip to first unread message

aetag...@gmail.com

unread,
Jan 13, 2016, 11:58:02 AM1/13/16
to web2py-users
I'm not sure if this is even possible, but if it is..how would something like this work:

You prompt the user for an x amount of days, then with javascript and jquery you have x amount of buttons appear. Each button is an instance of the form. There is a variable within the javascript that holds the amount x. So for each instance of x, the user fills out the same form but with different values for each day. In the db module containing the sqlform, there is a function that takes in each instance and performs what it needs to for each separate instance.

Also, if you are in views, how can you share a variable that is used in a script to python code in the same views, or in the db models file?

Massimo Di Pierro

unread,
Jan 14, 2016, 12:39:25 AM1/14/16
to web2py-users
def index():
    forms = [SQLFORM(db.table,formname='form%i' % k).process() for k in range(10)]
    for k,form in enumerate(forms):
       if form.accepted:
           print k, form.vars # or do something else
    return dict(forms = DIV(*forms))

Anthony

unread,
Jan 14, 2016, 10:02:37 AM1/14/16
to web2py-users
Are you wanting the user to fill all forms and submit them at once, submit one form at a time (but remaining on the same page in order to submit additional forms), or just submit one of the many forms and then have the page reload/redirect?

aetag...@gmail.com

unread,
Jan 18, 2016, 11:36:57 AM1/18/16
to web2py-users
Something like this: 
User inputs x amount of days, using jquery I will have buttons of some sort that represent x amount of days which appear when the user specifies x. They will then be able to click each button, where an instance of the form is displayed and they can enter information for that day then they can click another day and another instance of the same form generates. Once all forms have been filled accordingly, the user then submits the form at once. That is, if it is possible to do this way. 

I'm trying to take Massimo's example and work with that, only instead of specifying a range I need to link it to the form variable where the user submits the x amount so that the amount of form instances are created dynamically, not much success so far.

Anthony

unread,
Jan 18, 2016, 12:03:19 PM1/18/16
to web2py-users
This is a bit complicated. You don't actually want x separate forms, as you need to be able to submit a single form (so you want x copies of the input fields, but all wrapped in a single form). If you do that, though, for each field, you'll get a list of values (one for each copy of the field in the form). Alternatively, you could change the name of each input to include the day number. Either way, you'll need custom backend code to process all the submitted data and do the database inserts.

Another option would be to load a set of separate forms via Ajax (possibly as web2py components), and when the user is ready to submit, run some Javascript that programmatically submits each form separately via Ajax. In that case, the backend code would be very simple (just a single standard SQLFORM) -- all the complexity would be in the Javascript. This is a bit less efficient, as you'll get a separate HTTP request for each day's form.

Anthony

aetag...@gmail.com

unread,
Jan 18, 2016, 12:30:43 PM1/18/16
to web2py-users
For the first method, will I be able to essentially map each instance of form values to their database inserts? Kind of like if the user enters 5 days, 5 copies of form fields are made. The first copy maps to the first day, second copy maps to second day, etc?

As for the second method, if I am understanding correctly..if I were to load a set of separate forms via Ajax does that mean I would have to have multiple forms defined therefore I wouldn't be able to generate the instances dynamically? 

Anthony

unread,
Jan 18, 2016, 12:46:19 PM1/18/16
to web2py-users
On Monday, January 18, 2016 at 12:30:43 PM UTC-5, aetag...@gmail.com wrote:
For the first method, will I be able to essentially map each instance of form values to their database inserts? Kind of like if the user enters 5 days, 5 copies of form fields are made. The first copy maps to the first day, second copy maps to second day, etc?

As for the second method, if I am understanding correctly..if I were to load a set of separate forms via Ajax does that mean I would have to have multiple forms defined therefore I wouldn't be able to generate the instances dynamically? 

On the backend, you just have a function that defines a single form. Then in the browser via Javascript, you make Ajax calls to that function and keep retrieving new copies of the form via Ajax. You also submit each form separately via Ajax. This can all be done dynamically in Javascript.

Anthony

Richard Vézina

unread,
Jan 18, 2016, 1:20:30 PM1/18/16
to web2py-users
I think, he just try to figure out how to unlock the multiples inputs values for a given fields where he don't want to use the denormalized way proposed by web2py with list:string. I mean when you want a normalized db schema with a many-to-many relation for use case like tagging... There is not that much help on built-in web2py feature orther than create you own customized form for doing it...

I don't understand why since all that time we never come with a slice/howto/book section/etc to provide some guidance...

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages