Friday, April 12, 2019

Girls Who Code Lou Person Process Input Form

In this lesson, we will create a form in HTML which will submit the data to a script in Python to be processed.  You will create 2 files.  The first file will be the form and it will be in html.  The second file will be a python script which will go in the cgi-bin directory.

Name the html form formyou.html where you is your name.  After you upload it, access it here: http://gwc.daisyware.com/formyou.html
When you create the html file, you'll notice a reference to the script in the cgi-bin directory.  Change the name of the script to formyou.py where you is your name

Here is the form for the the html directory.  You'll name this file formyou.html

<!--change where it says form.py to formyou.py where you is your name, e.g. formlou.py-->
<form action="/cgi-bin/form.py" method="post">
FirstName: <input type="text" name="first_name">
LastName: <input type="text" name="last_name">
<input type="submit" value="go">
</form>

Here is the code for the cgi-bin directory.  You'll name the file formyou.py, e.g. formlou.py.  Make sure to change you in formyou.py to your name.

#!/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print("Content-type:text/html")
print
print("")
print("")
print("Hello")
print("")
print(" Hello %s %s" % (first_name, last_name))
print("<br>")
print(first_name, last_name)
print("")
print("")

No comments:

Post a Comment