Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Web Forms with Python
- 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.
- A form is a set of input fields contained in form HTML tags. The following are types of form fields:
- text area
- text input
- radio buttons
- check boxes
- drop down menus
- legend
- file select
- password
- submit button
- reset button
- 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.
- 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.
- Text Area
- Text areas allow users to enter a large amount of text like a blog post.
- HTML code:
- <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>
- Python code:
- #!/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)
- Text Input
- A text input field can be used to enter a single line of text.
- HTML code:
- <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>
- Python code:
- #!/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)
- Radio buttons
- Radio buttons allow users to pick one out of several options.
- HTML code:
- <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>
- On
- Off
- Python code:
- #!/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)
- Check box
- Users can pick one or more options
- HTML code:
- <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>
- Executable
- Python code:
- #!/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
- Drop down menu
- Users can pick an option from a drop down menu.
- HTML code:
- <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>
- My favaourite programming language is
- Python code:
- #!/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
- Legend
- HTML code:
- <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
- paragraphs.<p> </fieldset> </form>
- Legend
- These paragraphs are surrounded by a legend.
- You can use legends to group things together like these two
- paragraphs.
- Password field
- Like a text input box, but characters are obscured as they're typed in.
- HTML code:
- <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>
- Username: Password:
- Python code:
- #!/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