简体   繁体   English

如何使我的页面在从下拉菜单或文本字段获取数据之间进行选择?

[英]How would I make my page choose between getting data from a drop-down menu or a text-field?

thanks to some help from yesterday from stackoverflow, I've made progress in my code. 感谢昨天从stackoverflow获得的一些帮助,我的代码取得了进步。 But I have a question, concerning my page. 但是我有一个关于我的页面的问题。 Here is my code 这是我的代码

#!/usr/bin/python
print 'Content-type: text/html\n'
print

#May 17
import cgi, cgitb
cgitb.enable()

form=cgi.FieldStorage()

#May19
dataset1=open('Book1.csv','r')
dataset2=open('Race Demographic by state.txt','r')

sources='''<hr><h4>Sources!</h4><a     href="http://en.wikipedia.org/wiki/Demographics_of_the_United_States">Race Demographics</a>
<a href="http://voices.yahoo.com/average-sat-scores-state-2010-7702520.html?cat=4">SAT     Scores</a>
<a href="http://lisa.stuy.edu/~keiran.carpen/BriansDirectory/Sat scores by     state.txt">SAT Scores txt file</a>
<a href="http://lisa.stuy.edu/~keiran.carpen/BriansDirectory/Race Demographic by     state.txt">Race Demographics txt file</a></body></html>'''

def datasplitter(x):
    data = x.read()
    return data.split("\n")


def datdatamang(x):
    data = datasplitter(x)
    index = 0
    WhileNumber = 0
    while index < len(data):
        WhileNumber = 0
        string = data[index]
        string.split(" ")
        x=''
        while WhileNumber < len(string):
            if string[WhileNumber] == ',':
                x=x+'</td><td>'
            else:
                x=x+string[WhileNumber]
            WhileNumber+= 1
        ' '.join(string)
        data[index]='<tr><td>'+x+'</td></tr>'
        index+=1
    result=' '.join(data)
    result='''<table border='1'>'''+result+'</table>'
    return result

#May 19
def getDescription():
    page = ''
    state = ''
#May20
    if 'Drop' in form:
        if form['Drop'].value =="Description":
            page+= 'Ever since its conception, the SAT examinations have been widely     controversial.Many individuals believe that their race is the most intellectually superior,     and that their SAT scores reflect on their superiority. It is my job to find this     correlation between scores and race and end the battle among the races!'
        if form['Drop'].value=="High SAT Scores":
            page+= datdatamang(dataset1)
        if form['Drop'].value=="Race Demographics":
            page+= datdatamang(dataset2)
        if form['Drop'].value=="source":
            page+= sources
        else: 
    return page

#May 21
def getState():
    table=''
    if 'on' in form:
        state+= form['on'].value
    if state in dataset1:
        state.header=dataset1.index(state)
        for n in dataset1[state.header]:
            table+='''<tr>'''+n+'''</tr>'''
        return '''<td>'''+table+'''</td>'''


def getRacebyState():
    if 'on' in form:
        state+= form['on'].value
    if state in dataset2:
        state.header=dataset2.index(state)
        for n in dataset2[state.header]:
            table+='''<tr>'''+n+'''</tr>'''
        return '''<td>'''+table+'''</td>'''




#May 20
print '''<html><title>SAT Analysis</title>'''
print '''<body>'''
print getDescription()
print '''</body></html>'''



#May 17 - Writing the analysisV2.html file and starting the code
#May 19 - Tables, and the like
#May 20 - Writing if statements for drop-down menu, building the page.
#May 21 - Working on text fields and getState

Essentially, my page works so that you have a drop-down menu to choose from (choosing one of these values: eg "High SAT Scores" and "Race Demographics" causes my python code to generate a page that with tables, or descriptions concerning the drop-down menu option), or a text field (which searches for a state in my CSV files, and returns a table-row with the data about that particular State). 本质上,我的页面工作正常,因此您可以从下拉菜单中进行选择(选择以下值之一:例如,“ SAT高得分”和“种族人口统计”会导致我的python代码生成带有表格或相关说明的页面下拉菜单选项)或文本字段(在我的CSV文件中搜索状态,并返回包含该特定状态的数据的表行)。 Using cgi.FieldStorage() Python collects the value that is submitted through the HTML form. 使用cgi.FieldStorage()Python收集通过HTML表单提交的值。 However, how do I write the code so that I only send a value from the text-field only, through the HTML form. 但是,如何编写代码,以便仅通过HTML表单仅从文本字段发送值。

Ie if I do not want to use the drop-down menu, and instead only want to use the text-field to find a state in particular and not submit form data through the drop-down menu, how do I do that? 即,如果我不想使用下拉菜单,而是只想使用文本字段来查找特定状态,而不想通过下拉菜单提交表单数据,该怎么办?

I am not sure whether I am missing something here but, assuming 'on' is the name of your text field, you can check to see whether the text field is empty like so: 我不确定在这里是否丢失了某些内容,但是假设文本字段的名称为“ on”,则可以像下面这样检查文本字段是否为空:

if 'on' in form and form['on']:
    doSomethingWithOn()
else:
    doSomethingWithDrop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM