Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Use cookie in form (http://www.programmingforums.org/showthread.php?t=11970)

public2 Nov 22nd, 2006 3:15 AM

Use cookie in form
 
Hi there.

I'm working on a Python program where I load a form with text fields and then save input as a cookie. I print my HTML code by doing:

:

print content-type: text/html
print '''
<html><head><title>......
<form>
  <input name="replace" type="text" size="25" value=""/>
</form>.....'''
if os.environ.has_key('HTTP_COOKIE'):
    cookie.load(os.environ['HTTP_COOKIE'])
    if cookie.has_key('url') and cookie.has_key('find') and cookie.has_key('replace'):
      print cookie['mycookie']
print '</body></html>'


I have two questions. One is that my cookies are not replaced by the newest search words. I just have the first cookie from the first search I did with my script.

Second question and hurtle is, that I want my value="" in my form to have the last word I've entered in my text field, that is my cookie for "mycookie". I can post the whole code if it's easier for you to see my problem and/or explain what I'm doing wrong.

Greetings Public2

public2 Nov 23rd, 2006 1:52 AM

Have found a solution...

Dietrich Nov 26th, 2006 12:58 PM

Quote:

Originally Posted by public2 (Post 119561)
Have found a solution...

... and the solution is ...

public2 Nov 27th, 2006 2:14 AM

The solution was:

Question 1: To make a cookie from my input forms, I had to set it up like this:

:

cookie = Cookie.SimpleCookie()
    if form.has_key('form1') and form.has_key('form2') and form.has_key('form3'):
      cookie['form1'] = form['form1'].value
      cookie['form1']['expires']=25920000
      cookie['form2'] = form['form2'].value
      cookie['form2']['expires']=25920000
      cookie['form3'] = form['form3'].value
      cookie['form3']['expires']=25920000
      print cookie


Then i use my cookies as arguments when I call my HTMLbody function:

:

HTMLbody(cookie['form1'].value,cookie['form2'].value,cookie['form3'].value)

I my HTML body, I write my HTML out, and use my cookies with the %-function, like this:

:

<div>
<label for="t1">FORM1:</label><input type="text" name="form1" id="t1" value="%s"/><br />
<label for="t2">FORM2:</label><input type="text" name="form2" id="t2" value="%s"/><br />
<label for="t3">FORM3:</label><input type="text" name="form3" id="t3" value="%s"/><br />
</div>

Notice that I my value fields, I've used %s (that return a string). What to put inside my %s I define after my HTML code, so it looks like this:

:

<div>
<label for="t1">FORM1:</label><input type="text" name="form1" id="t1" value="%s"/><br />
<label for="t2">FORM2:</label><input type="text" name="form2" id="t2" value="%s"/><br />
<label for="t3">FORM3:</label><input type="text" name="form3" id="t3" value="%s"/><br />
</div>
</fieldset>
<div><input type="submit" value="SEARCH" id="submit" /></div>
</form>
</center>
</body></html>''' %(form1cookie,form2cookie,form3cookie)



All times are GMT -5. The time now is 12:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC