form in layout.html?

125 views
Skip to first unread message

Julius Minka

unread,
Jun 18, 2010, 12:26:40 PM6/18/10
to web2py-users
I need a contact form (name, address,...) on every page of a web site.
Thinking about putting the form into layout.html and use some Ajax to
send entered values to be processed by a controller. I would like to
avoid page reload on form submission.

I found 2 possible solutions in the archive:
1.
http://groups.google.com/group/web2py/browse_thread/thread/f162f35e4a84ee48/11524d762a9b1356
Is this incomplete? How process function is called?

2.
http://groups.google.com/group/web2py/browse_thread/thread/82fad17e26e1739a/c6a9cfd878653969
I ran application mentioned in the thread. This is probably close, but
complicated.

What is good and simple approach to this issue? I do not have much
experience with Ajax.

Thanks
Julius


mdipierro

unread,
Jun 19, 2010, 3:41:19 AM6/19/10
to web2py-users
Just do

def contact():
form=SQLFORM.factory(....)
if form.accepts(....)
return form # not dict(form=form)

and in layout.html

{{=LOAD('default','contact')}}




On Jun 18, 11:26 am, Julius Minka <j...@minka.sk> wrote:
> I need a contact form (name, address,...) on every page of a web site.
> Thinking about putting the form into layout.html and use some Ajax to
> send entered values to be processed by a controller. I would like to
> avoid page reload on form submission.
>
> I found 2 possible solutions in the archive:
> 1.http://groups.google.com/group/web2py/browse_thread/thread/f162f35e4a...
> Is this incomplete? How process function is called?
>
> 2.http://groups.google.com/group/web2py/browse_thread/thread/82fad17e26...

Julius Minka

unread,
Jun 19, 2010, 8:36:10 AM6/19/10
to web...@googlegroups.com
Thank You,

that is really simple.

Julius

V Sobota, 19. jún 2010 o 00:41 -0700, mdipierro napísal(a):

Julius Minka

unread,
Jun 19, 2010, 12:23:36 PM6/19/10
to web...@googlegroups.com
def form():
form=SQLFORM.factory(
Field('name', requires=IS_NOT_EMPTY()))
if form.accepts(request.vars, session):
response.flash = 'form accepted'
else:
response.flash = 'form has errors'
return form

Flash is not displayed in this place, elsewhere is working. Why?
Or, how let user know about the result of form submission?


V Sobota, 19. jún 2010 o 00:41 -0700, mdipierro napísal(a):

Giuseppe Luca Scrofani

unread,
Jun 19, 2010, 5:32:56 PM6/19/10
to web...@googlegroups.com
I need this solution too... Maybe with some help from javascript? Some
days ago testing various appliances from the repository I encountered
an example of submit button, when clicked it will open a dialog with a
personalized message. I will try to find it again...

(the typical flash response however is best, though)

mdipierro

unread,
Jun 20, 2010, 10:15:56 AM6/20/10
to web2py-users
Because the form is submitted and returned via ajax the usual
mechanism does not work but you can do:

def form():
form=SQLFORM.factory(
Field('name', requires=IS_NOT_EMPTY()))
if form.accepts(request.vars, session):
response.headers['web2py-component-flash'] = 'form accepted'
else:
response.headers['web2py-component-flash'] = 'form has errors'
return form

I.E. place the flash in a HTTP header and web2py will read it and
place it in the flash box.

Iceberg

unread,
Jun 21, 2010, 11:10:02 AM6/21/10
to web2py-users
Wow, thanks very much for sharing this trick. I knew it could be easy
and elegant! :-)

mdipierro

unread,
Jun 21, 2010, 12:44:50 PM6/21/10
to web2py-users
You can also do

if response.flash: response.headers['web2py-component-
flash']=response.flash

before return and you can keep using response.flash and session.flash
as usual.

sebastian

unread,
May 9, 2012, 5:45:08 PM5/9/12
to web...@googlegroups.com
in default.py I have

def search_bar():
    form
= FORM('Search  ',INPUT(_name='qry'))
   
if form.process().accepted:
        response
.flash=''
       
if form.vars.qry:
            redirect
(URL('services', vars={'qry':form.vars.qry}))
   
return form

and in layout.html

{{=LOAD('default','search_bar')}}

but for some reason the form is never accepted...

anh ideas ?

thanks

Anthony

unread,
May 9, 2012, 6:06:00 PM5/9/12
to web...@googlegroups.com
As it is, your component is not an Ajax component, and it is also not set up to trap form submissions via Ajax. So, when you submit the form, it will be submitted to the function of the parent page, not to the search_bar function. If you want the component to be loaded via Ajax, then set ajax=True (and the form will automatically be submitted via Ajax to the search_bar function). If you don't want an Ajax component but want the form submitted to the search_bar function, then set ajax_trap=True -- that will trap the form submission and submit it via Ajax to search_bar. Another option would be to explicitly set the form's "action" attribute to submit to the search_bar action, but in that case the entire page will reload with the response from search_bar, which presumably is not what you want.

Anthony

Sebastian E. Ovide

unread,
May 9, 2012, 6:23:53 PM5/9/12
to web...@googlegroups.com
the ajax_trap=True fixed the form which nows call search_bar... but redirect(URL('services', vars={'qry':form.vars.qry}))  is rendered inside the component....

What I'm trying to do, is to include a simple form in the layout.html that calls a function (as it is now, with the ajax_trap trick), and if it is not empty, it will redirect to a different page (loading the whole page)....

Anthony

unread,
May 9, 2012, 10:18:13 PM5/9/12
to web...@googlegroups.com
On Wednesday, May 9, 2012 6:23:53 PM UTC-4, sebastian wrote:
the ajax_trap=True fixed the form which nows call search_bar... but redirect(URL('services', vars={'qry':form.vars.qry}))  is rendered inside the component....

What I'm trying to do, is to include a simple form in the layout.html that calls a function (as it is now, with the ajax_trap trick), and if it is not empty, it will redirect to a different page (loading the whole page)....

If you want an Ajax request to result in a redirect of the whole page, you have to do it via Javascript after the Ajax response arrives. You could add something like this to the search_bar function:

    if [some condition]:
        form
= SCRIPT('jQuery(function() {location = "%s"})' %
            URL
('default', 'services', vars=dict(qry=form.vars.qry), extension=False))

That will replace the form with a script that changes the window location to the desired URL. Note, in the URL() function, extension is set to False -- otherwise, if the component happens to have the .load extension, that extension will propagate to the services URL -- setting extension to False simply leaves the extension off (in which case, it will look for the .html view).

Anthony
Reply all
Reply to author
Forward
0 new messages