forms in web2py

28 views
Skip to first unread message

Nazgi

unread,
Apr 25, 2009, 5:21:49 AM4/25/09
to web2py Web Framework
Hi,

I hav written a form in controller of my application. The form
has a select option which based on its input uses ajax and calls a
function. This function sends a json list object. How do I access that
variable in order to incorporate the contents of that list as another
select option menu.?

Thanks.

newbie

unread,
Apr 25, 2009, 4:51:08 PM4/25/09
to web2py Web Framework
in Controller/index.py:
def getcities():
citystr = request.vars.values()[0]
if citystr == "Delhi":
output = "Hyderabad"
elif citystr == "Tamil Nadu":
output = "Chennai"
elif citystr == "Andhra Pradesh":
output = "Pune"
else:
output = "mumbai"
return output

def index1():
form=FORM(TABLE(
TR("State:", SELECT("Andhra
Pradesh","Delhi","Tamil Nadu",_name="States",
_id="States",default="Delhi",_onchange="ajax('/Test/default/getcities',
['States'],'handle')",requires=[IS_NOT_EMPTY()])),
TR(DIV(_id="handle")),
TR("District", SELECT("----","asfas","safee",
_name="Choose", _id="district", default="----", requires=[IS_NOT_EMPTY
()]))))
return dict(form=form)


This is how i've written a form with ajax to auto-populate SELECT
boxes. But in the above code, if i want to use the "handle" variable
and display it as an option in SELECT box, I'm not able to do that. If
i return a list into the "handle" variable then how do i populate the
list in SELECT box. Please reply me asap.

Thanks,
Nazgi.

mdipierro

unread,
Apr 25, 2009, 6:58:27 PM4/25/09
to web2py Web Framework
Not sure I understand but you can try:

def getcities():
citystr = request.vars.values()[0]
if citystr == "Delhi":
output = "Hyderabad"
elif citystr == "Tamil Nadu":
output = "Chennai"
elif citystr == "Andhra Pradesh":
output = "Pune"
else:
output = "mumbai"
return TAG[''](OPTION("----"),OPTION("asfas"),OPTION
("safee"),OPTION(output))

def index1():
form=FORM(TABLE(
TR("State:", SELECT("Andhra
Pradesh","Delhi","Tamil Nadu",_name="States",
_id="States",default="Delhi",_onchange="ajax('/Test/default/
getcities',
['States'],'district')",requires=[IS_NOT_EMPTY()])),
TR("District", SELECT("----","asfas","safee",
_name="Choose", _id="district", default="----", requires=[IS_NOT_EMPTY
()]))))
return dict(form=form)

newbie

unread,
Apr 28, 2009, 1:59:01 AM4/28/09
to web2py Web Framework, mdip...@cs.depaul.edu, timmic...@gmx-topmail.de
I think I've messed it up. I would like to explain my problem once
again clearly.

in controllers/identity.py:
def getcities():
citystr = request.vars.values()[0]
if citystr == "Delhi":
output = "city1, city2" # list
elif citystr == "Tamil Nadu":
output = "city2, city3" # list
elif citystr == "Andhra Pradesh":
output = "city4, city5. city6" # list
else:
output = "Bombay"
return output


def index():
form=FORM(TABLE(
TR("State:", SELECT("Andhra
Pradesh","Delhi","Tamil Nadu",_name="States",
_id="States",default="Andhra Pradesh",_onchange="ajax('/Test/default/
getcities',['States'],'handle')",requires=[IS_NOT_EMPTY()])),
TR(DIV(_id="handle")),
TR("District", SELECT({{=handle}}, _name="Choose",
_id="district", default="----", requires=[IS_NOT_EMPTY()]))))
return dict(form=form)


What i'm trying to do here is: I've built a form to enter the states
and based on the option selected by the user, I'm generating another
option list. So in the first options menu, the user input will be sent
to geticities() function using ajax which will return a json object
list of cities beloging to that input entry. Now when I use that list
(handle) in DIV tag, I'm able to display list. But I'm not able to use
that list to populate the options in second SELECT tag as shown above.
Its giving me an error on writing so. Had i written that code for the
form in a View using html, i wud not have faced these problems. Since
I'm using the SELECT tag here, I'm facing these problems.
> > This is how i've written a form withajaxto auto-populate SELECT
> > boxes. But in the above code, if i want to use the "handle" variable
> > and display it as an option in SELECT box, I'm not able to do that. If
> > i return a list into the "handle" variable then how do i populate the
> > list in SELECT box. Please reply me asap.
>
> > Thanks,
> > Nazgi.
>
> > On Apr 25, 2:21 pm, Nazgi <mara.ku...@gmail.com> wrote:
>
> > > Hi,
>
> > >        I hav written a form in controller of my application. The form
> > > has a select option which based on its input usesajaxand calls a

Nithin Kumar M

unread,
Apr 28, 2009, 6:32:59 AM4/28/09
to web2py Web Framework
Hi,

         Thanks for your replies. I actually messed with some paths in my code. So it was accessing some old code and hence the new code wasnt working. Anyhow, thank you very much for your reply mdipierro. I'm able to solve my problem. :)
   
regards,
Nithin Kumar M.

DenesL

unread,
Apr 28, 2009, 11:10:07 AM4/28/09
to web2py Web Framework
Nice example of a cascaded field.
Reply all
Reply to author
Forward
0 new messages