I see. I saw you were making an Ajax post request and assumed that was supposed to be the form submission. If you're just using Ajax to pull the form html, you can do a get request (the default if you don't specify a method). I think the problem is that when you first call form.accepts when submittest is loaded, it adds the _formname and _formkey hidden fields to the form. However, the form ultimately displayed on the page and submitted is generated by ajax(), and does not include the _formname or _formkey hidden fields. So, when the form is submitted back to submittest, form.accepts fails because of the missing hidden fields. There's probably a more elegant way to handle this, but keeping as close to your original code as possible, I think it would be something like this: def submittest(): script = SCRIPT(""" function lf() { jQuery.ajax({url: 'ajax', success: function(msg) {jQuery('#chk').html(msg); } })}; """) # note, no need to make it a POST request menu = MENU([['LoadForm',False,'javascript:lf()']]) form = FORM(_name='myform') result = 'No result' if request.vars and form.accepts(request.vars,session): # only call .accepts upon submission result = request.vars.test return dict(menu=menu, script=script, result=result) # no need to return 'form' or display in view def ajax(): checkboxes = [DIV(INPUT(_type='checkbox',_name='test', _value=v, ),v) for v in ['a','b','c']] form=FORM(INPUT(_type="submit"), *checkboxes,_name='myform') form.accepts(request.vars,session) # this will add the hidden fields return form # no need to do str(form) -- returned helpers are automatically serialized Anthony
On Tuesday, August 23, 2011 1:11:03 PM UTC-4, mweissen wrote: > The whole demo program works fine: the form appears. If I add an > "action=..." in "FORM(INPUT(_type="submit"), *checkboxes,_name='myform')" > another page is called (as expected). > > The problem is that there is no value in request.vars. > > I don't know whether it is possible to add a form, which can be recognized > by "form.accepts". Something must be wrong, but I think, it is not > ajax-call.. > > 2011/8/23 Anthony <abas...@gmail.com> > >> I don't think you are using jQuery.ajax correctly. The url should be the >> url to which the submitted form data should be sent. Also, the jQuery.ajax >> call does not appear to actually send any data (it takes a 'data' argument). >> See >> http://code.google.com/p/web2py/source/browse/applications/welcome/static/js/web2py_ajax.js#54 >> for >> how web2py handles Ajax form submission. >> >> Anthony >> >> On Tuesday, August 23, 2011 11:04:01 AM UTC-4, mweissen wrote: >> >>> Hi, >>> >>> I want do create a "dynamic form": >>> (1) On the top of a page there are several questions and >>> (2) at the end of the page a form should appear. >>> >>> The content of the form depends on the answers of step (1). >>> Therefore I tried to insert this new form using ajax. >>> Result: the form appears, the submit-button works, but the content of >>> request.vars is always None. >>> >>> Here is the controller: >>> >>> def submittest(): >>> script = SCRIPT(""" >>> function lf() { >>> jQuery.ajax({type: 'POST', url: 'ajax', >>> success: function(msg) {jQuery('#chk').html(msg); } })}; >>> """) >>> >>> menu = MENU([['LoadForm',False,'javascript:lf()']]) >>> form = FORM(_name="myform") >>> if form.accepts(request.vars, session): >>> result = request.vars.test >>> else: >>> result = 'No result' >>> >>> return dict(menu=menu, script=script, result=result, form=form) >>> >>> def ajax(): >>> checkboxes = [DIV(INPUT(_type='checkbox',_name='test', _value=v, ),v) >>> for v in ['a','b','c']] >>> return str(FORM(INPUT(_type="submit"), *checkboxes,_name='myform')) >>> >>> And the view: >>> >>> {{extend 'layout.html'}} >>> <h1>Submit</h1> >>> {{=menu}} >>> {{=script}} >>> <div id='chk'>{{=form}}</div> >>> Result: {{=result}} >>> >>> Maybe the problem is the line form = FORM(_name="myform") >>> Without this line form.accepts does not work. Two forms, one from the >>> controller, one created by ajax? >>> >>> Regards, Martin >>> >> > >