如果用户在python中输入表单中的任何字段,如何搜索数据

时间:2013-11-01 05:46:17

标签: python cgi

cgi脚本下面包含文件中的搜索条件。表单有四个字段Fname,Lname,Age和Gender。如果用户在任何一个字段,两个字段,三个字段或四个字段中输入数据,则应显示匹配记录。 我怎么得到那个?对于所有领域。我怎么想为所有条件写作

#!/usr/bin/python
import os
import cgi


found = False

form = cgi.FieldStorage()
Fname = form.getvalue('firstname', '')
Lname = form.getvalue('lastname', '')
Age = form.getvalue('age', 0)
Gender = form.getvalue('gender', '')
fname = "/tmp/myfile.txt"

print "Content-type:text/html\n"
print "<html>"
print "<head>"
print "</head>"
print "<body>"
print "<h2><b><i>Search Data</i></b></h2>"
print "<table cellpadding='2' cellspacing='2' border='1'>"
print "<br>"
print "<td>First Name</td>"
print "<td>Last Name</td>"
print "<td>Age</td>"
print "<td>Gender</td>"
print "</tr>"


if os.path.exists(fname):

    f = open(fname, "r")

    b = []
    for line in f:
        temp = line.split()
        Fsearch = temp[0]
        Lsearch = temp[1]
        Asearch = temp[2]
        Gsearch = temp[3]



        if Fname and Fname.lower() == Fsearch.lower():
            found = True

        if Lname and Lname.lower() == Lsearch.lower():
            found = True

        if Age and Age == Asearch:
            found = True

        if Gender and Gender == Gsearch :
            found = True

        if found:
            b.append(line)


        found = False

    f.close()

    if len(b) == 0:
        print "<h2>", "No records found","</h2>"

    else:
        for each in b:
            store = each.split()

            print "<tr>"
            print "<td>", store[0], "</td>"
            print "<td>", store[1], "</td>"
            print "<td>", store[2], "</td>"
            print "<td>", store[3], "</td>"
            print "</tr>"


else:
    print "Search Other File"
                                                                                           print "</table>"
print "<br><a href='/~tom/index.html'>Home</a>"
print "</body>"
print "</html>"    

我有一个这样的文件:

Ram Charan 25 male
John Mckay 28 Male
Ashley Lobo 25 Female
Sara jane 28 Female

现在我的forn ha四个领域:

FirsrName:
Last Name:
Age:
Gender(male Female Radio button)

如果用户在任何一个字段或两个字段或所有字段中输入数据,则它应匹配数据并显示记录

对于例如我输入:

Firstname: 
lastName:
Age: 25
Gender: Male (radio button)

应显示25岁男性和男性的记录

如果用户在任何三个字段中输入数据:

FirstName: Ashley
Lastname: 
Age: 25
Gender: Female (radio button)

它应该显示匹配的记录。

同样,如果用户在所有字段中输入数据并且明智地

0 个答案:

没有答案