[web2py] Re: why is form self submit getting executed on cancel? [Closed]
> > >>> --
[web2py] Re: why is form self submit getting executed on cancel?
@Cliff, thanks for the suggestion but it doesn't work because the onclick event can not be associated with the insert method. Likewise, for the reason stated by Anthony, it is the return false that is needed to prevent the update action from executing. @Anthony, thanks, as usual, for your suggestions and illuminating responses. As expected, your suggestion works. On Monday, June 25, 2012 8:35:49 PM UTC+8, Anthony wrote: > > The onclick event doesn't cancel the standard submit behavior -- it just > adds the onclick behavior before submitting. If you want to prevent the > submit action, do: > > _onclick='ajax("%s",[],":eval");return false' > > "return false" prevents the submit. An alternative to using onclick is to > register a jQuery event handler and call preventDefault() at the end of the > handler. That is sometimes preferable because although it prevents the > submit, it does not stop the propagation of the event itself, in case > there are other handlers set up to catch the same event ("return false" > will prevent any other event handlers from being triggered). > > Anthony > > On Monday, June 25, 2012 6:13:31 AM UTC-4, weheh wrote: >> >> My controller has a componentized form with an added Cancel button >> like this (this is an excerpt, so sorry for any typos): >> >> def update( ): >> >> myform = SQLFORM.factory( >> Field('text_in','text',requires=IS_NOT_EMPTY()), >> buttons=[INPUT(_type='submit',_value=T('Update'))], >> _id='myform', >> formstyle='divs' >> ) >> form.element('input[type=submit]').parent.append( >> TAG.BUTTON(T('Cancel')),_onclick='ajax("%s",[],":eval");' % URL( >> c='my_controller', f='cancel_update') >> ) >> if myform.process(formanem='myform').accepted: >> ...do stuff... >> return dict(myform=myform) >> >> >> >> The form submits >> properly when the Update button is pressed. However, when the Cancel >> button is pressed, not only does >> the "cancel_update" callback get called, the form also gets submitted >> erroneously. The question is, why does the "update" action get called and >> its form get submitted at all when the Cancel button is pressed? >> >> --
[web2py] Re: why is form self submit getting executed on cancel?
The onclick event doesn't cancel the standard submit behavior -- it just adds the onclick behavior before submitting. If you want to prevent the submit action, do: _onclick='ajax("%s",[],":eval");return false' "return false" prevents the submit. An alternative to using onclick is to register a jQuery event handler and call preventDefault() at the end of the handler. That is sometimes preferable because although it prevents the submit, it does not stop the propagation of the event itself, in case there are other handlers set up to catch the same event ("return false" will prevent any other event handlers from being triggered). Anthony On Monday, June 25, 2012 6:13:31 AM UTC-4, weheh wrote: > > My controller has a componentized form with an added Cancel button > like this (this is an excerpt, so sorry for any typos): > > def update( ): > > myform = SQLFORM.factory( > Field('text_in','text',requires=IS_NOT_EMPTY()), > buttons=[INPUT(_type='submit',_value=T('Update'))], > _id='myform', > formstyle='divs' > ) > form.element('input[type=submit]').parent.append( > TAG.BUTTON(T('Cancel')),_onclick='ajax("%s",[],":eval");' % URL( > c='my_controller', f='cancel_update') > ) > if myform.process(formanem='myform').accepted: > ...do stuff... > return dict(myform=myform) > > > > The form submits > properly when the Update button is pressed. However, when the Cancel > button is pressed, not only does > the "cancel_update" callback get called, the form also gets submitted > erroneously. The question is, why does the "update" action get called and > its form get submitted at all when the Cancel button is pressed? > > --
[web2py] Re: why is form self submit getting executed on cancel?
How about something like, myform[0].insert(-1, TAG.BUTTON(T('Cancel')),_onclick= 'ajax("%s",[],":eval");' % URL( c='my_controller', f='cancel_update') ) If you want the cancel button to appear in the same table row as the submit button, you may need something like myform[0][1]... On Monday, June 25, 2012 6:13:31 AM UTC-4, weheh wrote: > > My controller has a componentized form with an added Cancel button > like this (this is an excerpt, so sorry for any typos): > > def update( ): > > myform = SQLFORM.factory( > Field('text_in','text',requires=IS_NOT_EMPTY()), > buttons=[INPUT(_type='submit',_value=T('Update'))], > _id='myform', > formstyle='divs' > ) > form.element('input[type=submit]').parent.append( > TAG.BUTTON(T('Cancel')),_onclick='ajax("%s",[],":eval");' % URL( > c='my_controller', f='cancel_update') > ) > if myform.process(formanem='myform').accepted: > ...do stuff... > return dict(myform=myform) > > > > The form submits > properly when the Update button is pressed. However, when the Cancel > button is pressed, not only does > the "cancel_update" callback get called, the form also gets submitted > erroneously. The question is, why does the "update" action get called and > its form get submitted at all when the Cancel button is pressed? > > --