Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

exercise2.html <!DOCTYPE html> <html lang=\"en\"> <head> <meta charset=\"utf-8\"

ID: 3717072 • Letter: E

Question

exercise2.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 8 Exercise 2 Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Week 8 Exercise 2 PHP form demo</h1>
<form id="userinfo" action="exercise2.php" method="post">
<p>Please fill in the following form. All fields are mandatory.</p>

<p>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="firstname">
</p>

<p>
<label for="email">Email Address:</label>
<input type="text" id="email" name="email">
</p>

<p>
<label for="addr">Postal Address:</label>
<textarea rows="5" cols="300" id="addr" name="postaddr"></textarea>
</p>

<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport" size="4" multiple>
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
</p>

<p>
<label for="list">Add me to the mailing list</label>
<input type="checkbox" id="list" name="emaillist" value="Yes">
</p>

<p><input type="submit" value="submit"></p>
</form>
</body>
</html>

exercise2.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 8 Exercise 1</title>
</head>
<body>
<?php
//obtain the firstname input from the $_GET array
$namestr = $_GET["firstname"];
$namestr1 = $_GET["email"];
$namestr2 = $_GET["postaddr"];
$namestr3 = $_GET["favsport"];
$namestr4 = $_GET["emaillist"];
//obtain the values for the other input devices here

?>
<p>The following information was received from the form:</p>
<p><strong>name = </strong> <?php echo "$namestr"; ?></p>
<p><strong>email = </strong> <?php echo "$namestr1"; ?></p>
<p><strong>address = </strong> <?php echo "$namestr2"; ?></p>
<p><strong>favourite sport = </strong> <?php echo "$namestr3"; ?></p>
<p><strong>email list = </strong> <?php echo "$namestr4"; ?></p>
<!--output the other form inputs here -->
</body>
</html>

1. modify exercise2.php to correctly process the form from exercise2.html. Hint: Because of the changes made to the form (in particular, the change to the form method is critical) you will need to modify several aspects of the php script.

2. Ensure that your task2 script is capable of displaying all of the selections made from the selection list - it won’t without appropriate modifications to both the html form and the php.

Explanation / Answer

Following is the answer:

exercise2.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 8 Exercise 2 Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Week 8 Exercise 2 PHP form demo</h1>
<form id="userinfo" action="exercise2.php" method="post">
<p>Please fill in the following form. All fields are mandatory.</p>

<p>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="firstname">
</p>

<p>
<label for="email">Email Address:</label>
<input type="text" id="email" name="email">
</p>

<p>
<label for="addr">Postal Address:</label>
<textarea rows="5" cols="300" id="addr" name="postaddr"></textarea>
</p>

<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport[]" size="4" multiple>
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
</p>

<p>
<label for="list">Add me to the mailing list</label>
<input type="checkbox" id="list" name="emaillist" value="Yes">
</p>

<p><input type="submit" name="submit" value="submit"></p>
</form>
</body>
</html>

exercise2.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 8 Exercise 1</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
//obtain the firstname input from the $_GET array
$namestr = $_REQUEST["firstname"];
$namestr1 = $_REQUEST["email"];
$namestr2 = $_REQUEST["postaddr"];
$namestr3 = implode(',',$_REQUEST["favsport"]);
$namestr4 = $_REQUEST["emaillist"];


} //obtain the values for the other input devices here

?>
<p>The following information was received from the form:</p>
<p><strong>name = </strong> <?php echo $namestr; ?></p>
<p><strong>email = </strong> <?php echo $namestr1; ?></p>
<p><strong>address = </strong> <?php echo $namestr2; ?></p>
<p><strong>favourite sport = </strong> <?php echo $namestr3; ?></p>
<p><strong>email list = </strong> <?php echo $namestr4; ?></p>
<!--output the other form inputs here -->
</body>
</html>