Here we explore a bit more about the php programming language. Topics include:
Logical / boolean 'true' and 'false'
Comparison operators
The 'if' statement
$_GET, $_POST, and $_SERVER arrays
How to send emails from a php CGI: the mail() function
Note: I strongly suggest you view all our videos in full screen mode: click the icon
in the lower right corner of the video area (it shows four arrows pointing
to the corners of the screen). When you are done, the Esc ("escape") key
returns you to regular mode.
Lesson 07, part 1 More php: comparisons, 'if', and emails
This exercise is primarily focused on setting up a new CGI that will implement nested
'if' statements in the next exercise.
The Table 'N' Fable club has organized a trip to visit locations in Wales named in the
mystery novel "Welsh Rarebit Gone".
In lesson 4's lab, you constructed the HTML page 'signUp.shtml', which includes a form
that invokes a CGI named 'AddTraveler.php' in your 'cgis' directory.
So in this exercise you will code cgis/AddTraveler.php - a new CGI
Here are steps to construct your first cut at AddTraveler.php:
Open phpStarter.txt and save it as 'AddTraveler.php' in your AdvancedWebFiles/cgis directory and then:
replace "==purpose / goal of cgi" with "cgi to process a sign up for our trip to Wales"
replace "==how invoked (method, webpage)" with "invoked using POST from signUp.shtml"
add some comments regarding the goals of the CGI, such as:
Build user street address, two parts (you'll not write the code until the next exercise)
Build a response page, including the constructed street address
Send an email alerting you to the new sign up
under the comment "get variables from form "
retrieve all the variables from the form into php variables Important point: since this form includes a file upload (possible picture of applicant),
all variables must be accessed using $_POST, except the file, which is accessed using $_FILES
so use $_POST["name"]
Example: $first = $_POST["firstname"]; and $last = $_POST["familyname"];, etc.
except use $_FILES["pic"] for the file item with name 'pic'
under the comment "variables used in our work"
declare the following variables:
$mailto = - put your personal name and email there
$subject = "A new sign up for the Wales trip!"
$headers = "From: put your email address here"
$msg = "$first $last has signed up to join our trip to Wales.\n";
below the comment "/* build response page */", build the response page for the user:
use the 'echo' statements already there for 'doctype', 'html' start tag, and 'head' start tag then add the following:
echo a <title> element with value "Sign-up results"
echo </head>, an end head element tag
echo <body>, a body element start tag
echo an <h1> element with content "Welcome $first, thanks for signing up to join our trip to Wales!"
echo a <p> element with: "One of the trip leaders will contact you soon to answer questions"
echo </body>, a body end tag
echo </html>, an html end tag
after the echo of </html>, send your email message to yourself
Save the resulting AddTraveler.php file and upload it to your AdvancedWebFiles/cgis directory
Test your sign up form / cgi combination: verify you get the email as expected and the user sees the feedback page.
Session 3 Addresses
Here we explore displaying addresses on a website, along with another opportunity
to use nested if's.
This exercise is primarily focused using complex 'if' statements to
construct a correctly formatted street address, based on what data the user has
supplied. It modifies your new AddTraveler.php code.
In this exercise you will extend cgis/AddTraveler.php - your new CGI
Here are steps to update AddTraveler.php:
under the comment "variables used in our work" add the following variable declares:
$addr = "" - used for holding the street address we build
$addr_back = "" - to hold city, state, zip, country: the back end of the address
after the gathering of variables from the form, we're going to build / format the address as in the lecture, so
add a comment: "/* construct front / top end of address */"
the 'front end' is basically the street address, and we'll construct it in the variable "$addr":
if addr1 from the form is omitted, then $addr = "" and ignore $addr2
otherwise, $addr = address line 1 from the form
and if there is a value in address line 2 from the form then
$addr = first_line<br>second_line
then add a comment "/* construct back / bottom end of address */"
to build the back end of the address, we consider if there are values for city, state, mailcode and country:
if city and state have values: $addr_back = "$city, $state $zip";
if city has a value but state does not, then $addr_back = $city;
if city does not have a value but state does, then $addr_back = $state;
if country has a value and it's not "USA": $addr_back = $addr_back."<br>".$country;
Finally, if both $addr and $addr_back are empty, set $addr to "N/A"
after the echo of the </head> end tag,
after the text "One of the trip leaders will contact you soon to answer questions, " add
echo: "<br> and confirm the details as we have them.</p>"
echo a <p> element with "Here is the address we have for you:<br><br>"
echo "$addr<br>$addr_back</p>"
Save your updated AddTraveler.php and test it out, especially the formatting of addresses.
Supplemental materials
A cumulative summary of the contents of this course so far:
Click here
For a combined cumulative summary of both Web courses so far,
Click here
Email us
if you would like additional information.
We would love to see your comments and
recommendations for our site.
Our privacy
policy: we use cookies to keep track
of where you are on the site, but we do not
leave cookies on your site; we do not track
your visits; we do not disseminate any
information about you because we do not
gather any information about you.