Advertisement
johnmahugu

cgi:webforms

Dec 23rd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. Web Forms with Python
  2.  
  3. Web applications usually need get input from users at some point. Whether you're building a blogging site or a web UI for an embedded device, forms are a great way to allow users to interact with a web site.
  4.  
  5. A form is a set of input fields contained in form HTML tags. The following are types of form fields:
  6.  
  7. text area
  8. text input
  9. radio buttons
  10. check boxes
  11. drop down menus
  12. legend
  13. file select
  14. password
  15. submit button
  16. reset button
  17.  
  18. The form tag contains two attributes, the action attribute which specifies which script should execute when the submit button is clicked, and the method used to pass the form to the server, GET or POST.
  19.  
  20. When the user fills in a form and clicks the submit button, the form data is sent to the server and processed by the script in the form attribute.
  21. Text Area
  22.  
  23. Text areas allow users to enter a large amount of text like a blog post.
  24. HTML code:
  25.  
  26. <form action="/cgi-bin/examples/textarea.py" method="POST"> <textarea name="post" cols=70 rows=5></textarea><br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  27.  
  28.  
  29. Python code:
  30.  
  31. #!/usr/bin/env python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" form=cgi.FieldStorage() if "post" not in form: print "<h1>The text area was empty.</h1>" else: text=form["post"].value print "<h1>Text from text area:</h1>" print cgi.escape(text)
  32.  
  33. Text Input
  34.  
  35. A text input field can be used to enter a single line of text.
  36. HTML code:
  37.  
  38. <form action="/cgi-bin/examples/textinput.py" method="POST"> <input type="text" size="70" name="data" value=""><br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  39.  
  40.  
  41. Python code:
  42.  
  43. #!/usr/bin/env python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" form=cgi.FieldStorage() if "data" not in form: print "<h1>The text input box was empty.</h1>" else: text=form["data"].value print "<h1>Text from text input box:</h1>" print cgi.escape(text)
  44.  
  45. Radio buttons
  46.  
  47. Radio buttons allow users to pick one out of several options.
  48. HTML code:
  49.  
  50. <form action="/cgi-bin/examples/radiobuttons.py" method="POST"> <input type="radio" name="light" value="on">On<br> <input type="radio" name="light" value="off">Off <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  51.  
  52. On
  53. Off
  54. Python code:
  55.  
  56. #!/usr/bin/env python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" form=cgi.FieldStorage() if "light" not in form: print "<h1>Neither radio button was selected.</h1>" else: text=form["light"].value print "<h1>Radio button chosen:</h1>" print cgi.escape(text)
  57.  
  58. Check box
  59.  
  60. Users can pick one or more options
  61. HTML code:
  62.  
  63. <form action="/cgi-bin/examples/checkbox.py" method="POST"> <input type="checkbox" name="setting" value="set" > Executable <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  64.  
  65. Executable
  66. Python code:
  67.  
  68. #!/usr/bin/env python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" form=cgi.FieldStorage() if "setting" not in form: print "<h1>The box was not checked</h1>" else: text=form["setting"].value print "<h1>The check box was ticked:</h1>" print text
  69.  
  70. Drop down menu
  71.  
  72. Users can pick an option from a drop down menu.
  73. HTML code:
  74.  
  75. <form action="/cgi-bin/examples/dropdown.py" method="POST"> My favaourite programming language is <select name="language"> <option value="c">C</option> <option value="python">Python</option> <option value="php">PHP</option> <option value="Java">Java</option> </select> <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  76.  
  77. My favaourite programming language is
  78. Python code:
  79.  
  80. #!/usr/bin/env python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" form=cgi.FieldStorage() if "language" not in form: print "<h1>No option chosen</h1>" else: text=form["language"].value print "<h1>The option chosen was:</h1>" print text
  81.  
  82. Legend
  83. HTML code:
  84.  
  85. <form action="/cgi-bin/examples/legend.py" method="POST"> <fieldset> <legend>Legend</legend> <p>These paragraphs are surrounded by a legend.<p> <p>You can use legends to group things together like these two
  86. paragraphs.<p> </fieldset> </form>
  87.  
  88. Legend
  89.  
  90. These paragraphs are surrounded by a legend.
  91.  
  92. You can use legends to group things together like these two
  93. paragraphs.
  94.  
  95. Password field
  96.  
  97. Like a text input box, but characters are obscured as they're typed in.
  98. HTML code:
  99.  
  100. <form action="/cgi-bin/examples/password.py" method="POST"> Username: <input type="text" name="user_name" value=""> Password: <input type="password" name="password"> <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>
  101.  
  102. Username: Password:
  103. Python code:
  104.  
  105. #!/usr/bin/env python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" form=cgi.FieldStorage() if "user_name" not in form: print "<h1>No username was entered</h1>" else: text=form["user_name"].value print "<h1>Username:</h1>" print cgi.escape(text) if "password" not in form: print "<h1>No password was entered</h1>" else: text=form["password"].value print "<h1>Password:</h1>" print cgi.escape(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement