0

这是我的 form.html

form name="form1" action="/cgi-bin/form2.py" method="post"
input type="text" name="firstname"
input type="submit" value="submit"

下面是我的form2.py

import cgi
print 'Content-type:text/html\n\n'
cookie=Cookie.SimpleCookie()
form = cgi.FieldStorage()
searchterm = form["firstname"].value
print 'Set-Cookie: lastvisit=' + str(searchterm)
cookie["firstname"]=searchterm
print "firstname="+ cookie["firstname"].value
cookie_string = os.environ.get('HTTP_COOKIE')
if not cookie_string:
      print '<p>First visit or cookies disabled</p>'
 else: 
      print '<p>The returned cookie string was "' + cookie_string + '"</p>'
cookie.load(cookie_string)
lastvisit = float(cookie['lastvisit'].value)
cookie['lastvisit']['path'] = '/var/www/cgi-bin/'
print searchterm
print 'Content-type:text/html\n\n'
print "<meta http-equiv='refresh' content='5'>"

我们有一个 form.html 和 form2.py 文件,从 form.html 我提交 firstname 到 form2.py 提交后我得到'searchterm'值,然后我在刷新后刷新浏览器我得到<type 'exceptions.keyerror'>:firstname。即使我已经设置并检索了 cookie 值,我也无法使用 cookie 值刷新浏览器。请帮助我,我从过去 4 天开始就面临这个问题。

谢谢,维塔尔。

4

1 回答 1

0

如果我的猜测是正确的,首先你点击提交按钮,然后它重定向到 form2.py(即 form.html 正在向 form2.py 提交值,所以值 form["firstname"].value 存在)。现在您正在刷新 form2.py ,因为没有提交,所以它又不发送任何值。刷新并不意味着提交它只是加载当前页面

form2.py 没有提交任何数据,那么值 form["firstname"].value 是如何存在的

于 2013-04-25T12:18:58.883 回答