Saturday, April 27, 2019

Lou Person STEM Girls Who Code Select and Decide

In this lesson we will use the select input type of our form.  This let's us select a value from a picklist so the script will have the same value each time.  With the text field, the user can type whatever they want and the script will print it back.  With the select input type, the value is already entered in the html.

Name the html form formyou.html where you is your name.  After you upload it, access it here: http://gwc.daisyware.com/formyou2.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 formyou2.py where you is your name

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

<!--change where it says form.py to formyou.py where you is your name, e.g. formlou2.py-->
<form action="/cgi-bin/formlou2.py" method="post">
    FirstName: <input type="text" name="first_name">
    LastName: <input type="text" name="last_name">
    <select name="mood">
        <option value="happy">Happy</option>
        <option value="funny">Funny</option>
        <option value="tired">Tired</option>
        <option value="hungry">Hungry</option>
      </select>
    <input type="submit" value="go">
    </form>

Here is the code for the cgi-bin directory.  You'll name the file formyou2.py, e.g. formlou2.py.  Make sure to change you in formyou.py to your name.  Notice that we have a new variable called my_mood.  This is the value that the user selected in the html form using the select input type.  You'll also notice two variables called hungry_fix and tired_fix.  They each have a suggestion for the user on what to do about their mood.  Add others, such as happy_fix and funny_fix to your script after you get the base script working.  You can also add more options in the select field in the html form.  Then notice that we have an "if" statement.  The if statement looks at the mood the user selected and then prints the suggestion based on the mood.

#!/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
hungry_fix="Go to the deli"
tired_fix="Take a nap"
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
my_mood = form.getvalue('mood')
print("Content-type:text/html")
print
print("")
print("")
print("")
print(" Hello %s %s" % (first_name, last_name))
print("<br>")
if my_mood=="hungry":
    print hungry_fix
if my_mood=="tired":
    print tired_fix
print("")
print("")

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("")