Additional submit buttons on form

698 views
Skip to first unread message

Stephen Weiss

unread,
Aug 11, 2014, 8:11:19 AM8/11/14
to web...@googlegroups.com

I'm in search of a way to have multiple submit buttons on a form that would perform a different function depending on whichever button was pressed.

After pressing whichever button, the form should be submitted and after performing the function, return the user back to the original form.


I have a standard 'index' method that builds the form and adds another button like:

   form.add_button("Reset", URL("reset", args = dict(deploy_name = request.vars.deploy_name)))


When i press the Reset button, it calls the reset method I have defined, but I have two problems:

1. I can't seem to get the value of the deploy_name, which is a field on the form.
   Is args the proper method of passing variable information or should I be using something else?

2. I can't seem to redisplay the form as it originally started out as.
    I tried adding a redirect('index') call to the end of the reset method, but that causes strange operation.

Thanks!



lyn2py

unread,
Aug 11, 2014, 12:34:29 PM8/11/14
to web...@googlegroups.com
A few things…

1)
  • args takes a list
  • vars takes a dict
do you mean to use vars instead of args?

2) If deploy_name is a field in the form, and you want to get the value of it on clicking the button, you need to use javascript, like jQuery or the likes. 

3) Actually I am not sure what you want to achieve, so I can only guess, and I may be wrong. 
  • By default, web2py will submit the form to itself and return back the original form. You can call other functions from within that function. 
  • In your quoted case (the button's code), clicking on the button only takes the user to another page instead of submitting the form. 

Stephen Weiss

unread,
Aug 11, 2014, 12:52:14 PM8/11/14
to web...@googlegroups.com

Well, I'm VERY new to Web2Py, so at this point I've got a lot of learning to do...

After doing some more reading, I think I need to use vars.

Yes deploy_name is a field on the form from which I'd like to obtain user input.

I'm really not sure what the recommended approach to work this page within the Web2Py framework.

The page has a number of fields, some drop-down selections from db tables, another (deploy_name) a text field.

The deploy_name field can be used in different functions, thus I need multiple buttons. 

The standard Submit button, works as expected, but as you state, the Reset button does not.

However, I'd like the second (and possibly more buttons) to use the deploy_name field, which is why I'm redirecting to a different method.
It appears as though that's not the proper way of handling this scenario then?

Should all the buttons submit then instead of redirect?
Is there a way to determine in the controller code which button the user pressed?

Thanks!

lyn2py

unread,
Aug 11, 2014, 6:14:11 PM8/11/14
to web...@googlegroups.com
Maybe you can let us see your code and explain what you would like to do, if you would like some input on the direction of how to do it?

Stephen Weiss

unread,
Aug 12, 2014, 9:22:19 AM8/12/14
to web...@googlegroups.com

I discovered that you can add a buttons list to an SQLFORM.factory call.

The last post in this question has syntax https://groups.google.com/forum/#!searchin/web2py/buttons/web2py/Hu8iCzOA9Lk/lJnVtBLNvK8J

This allows for multiple submit buttons to the same controller code and in there you can test which button was pressed and act accordingly.

Thanks!
Reply all
Reply to author
Forward
0 new messages