[Tutor] html and mod_python

Kent Johnson kent37 at tds.net
Thu Mar 23 11:48:00 CET 2006


Patty wrote:
> Hi!
> 
> I created a form in a python file that takes values selected from 6 different 
> drop down boxes:
> 
> for target in all_targets:
>        s = s + "<form action='process_info' method='POST'>"
>        s = s + "<input type='hidden' name='tg' value=%s>" % target
>        s = s + "<tr><td></td>"
>        for host in hosts:
>            if target in ants_map[host]:
>                s = s + printbox()
>            else:
>              s = s + printemptybox()
>        s = s + """<td><input type='submit' value='Submit'></td></tr>"""
>        s = s + "</form>"
>        
> My printbox method contains the following:
> def printbox():
> 
>    tag = """
>    <select name="percent">
>    <option VALUE="-">-</option>
>    <option VALUE="0">0%</option>
>    <option VALUE="10">10%</option>
>    <option VALUE="20">20%</option>
>    <option VALUE="30">30%</option>
>    <option VALUE="40">40%</option>
>    <option VALUE="50">50%</option>
>    <option VALUE="60">60%</option>
>    <option VALUE="70">70%</option>
>    <option VALUE="80">80%</option>
>    <option VALUE="90">90%</option>
>    <option VALUE="100">100%</option>
>    </select>"""
>    return tag
> 
> When the values are entered, my program stores them into a database. 
> Everything worked well, but now I have been asked to retain the values 
> selected in the drop down boxes. This means that when I open the web page, the 
> drop down boxes should display the recent values entered. I'm not sure how to 
> do it, and I'm really hoping that someone can point me to the right direction. 

You have to add the attribute 'SELECTED' to the option tag that you want 
to be selected. To do this, you should recover the old value from the 
database and pass it to printbox(). Then change printbox() to create the 
tag using a loop over the value. For each value, check to see if it is 
the same as the value passed in and generate the appropriate option tag.

HTH
Kent



More information about the Tutor mailing list