Updated 2/23/2020 at 8:45pm
Homework #2: Conditions and Input
-
Remember to follow all the standard Homework Requirements.
-
This assignment will demonstrate:
-
Make a new page for your site, and name it contactus.php. This will be a
"Contact Us" page.
-
Put an HTML link on your index.php page, which allows someone to navigate to your new
"Contact Us" page.
-
Highlight and copy the HTML div, form, and PHP if-else-if code below. Save the code to the body of your
Contact Us page.
<div id="formDiv">
<?php
if (!isset($_POST["formSubmitted"]))
{
?>
<form name="theForm" action="contactus.php" method="post">
<input type="hidden" name="formSubmitted" value="1">
<h3>Please select a state:</h3>
<br>
<input type="radio" name="theState" value="OK"> OK
<br>
<input type="radio" name="theState" value="TX"> TX
<br>
<input type="radio" name="theState" value="VA"> VA
<br>
<input type="radio" name="theState" value="WA"> WA
<br>
<br>
<input type="submit" value="Expand your selection">
</form>
<?php
}
else
{
?>
<h3>The name of your state is:</h3>
<?php
echo "<h3>\n";
if (isset($_POST["theState"]))
{
$theState = $_POST["theState"];
}
else
{
$theState = '';
}
if ($theState == "TX")
{
echo "Texas";
}
else if ($theState == "OK")
{
echo "Oklahoma";
}
else if ($theState == "VA")
{
echo "Virginia";
}
else if ($theState == "WA")
{
echo "Washington";
}
else
{
echo "Unknown";
}
echo "</h3>\n";
echo "<p>Please <a href='contactus.php'>go back</a> and select another state.</p>\n";
}
?>
</div>
-
Make sure the action attribute in
the HTML <form> tag is the same as the filename for your Contact Us page,
which should be contactus.php.
-
Now change the Contact Us page so it uses a switch statement instead of
the current if-else-if code. To be more specific, you will be replacing this code:
if ($theState == "TX")
{
echo "Texas";
}
else if ($theState == "OK")
{
echo "Oklahoma";
}
else if ($theState == "VA")
{
echo "Virginia";
}
else if ($theState == "WA")
{
echo "Washington";
}
else
{
echo "Unknown";
}
with a switch statement that runs the proper echo statement
based on the value in the variable $theState.
-
Upload your Contact Us page to your class Web site.
-
Test your page at your Web site by clicking your student number on the
"Student Sites" page of this class site. Display your Contact Us page. Make a selection. Click the submit button
and make sure
the Contact Us page displays the correct value.