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

Write a program called week2.php that displays a form that submits to itself. Wh

ID: 3818966 • Letter: W

Question

Write a program called week2.php that displays a form that submits to itself. When initially accessed, week2.php displays an HTML form. If the the user POSTs the form, week2.php will simply output the names and values (if any) ofeach of the form elements, and exit. Use htmlspecialchars on the form data that you display. If one of your form fields is unset (i.e., the checkbox), output something like [no value] in place of the value. Copy the following from the class website and use it for your assignment, replacing the just the multi-line comment with the necessary PHP code 1. O iso-8859-1 ?php echo xml version encoding DOC TYPE html /W3C /DTD XHTML 1. O Strict /EN PUBLIC http:// w3. org/TR/xhtml 1 DTD/xhtml, 1-strict. dtd" http:// w3. org 1999 /xhtml

Explanation / Answer

Below is the complete HTML form code along with the PHP code to be inserted for the mentioned problem:

<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Web Development with PHP: Session Two</title>
   <style type="text/css">
   body{
       font-family: verdana, arial, helvetica, sans-serif;
       font-size: small;
       line-height: 120%;
       margin-left: 20%;
   }
   form, textarea{
       font-family: verdana, arial, helvetica, sans-serif;
       font-size: small;
   }
   </style>
   <meta http-equiv="Content-Type" content="text/html"; charset="iso-8859-1" />
</head>
<body>
<?php
   if ($_SERVER['REQUEST_METHOD'] == 'POST'){
       if(isset($_POST['your_name']))
           $your_name = $_POST['your_name'];
           else $your_name = '[no_value]';
       if(isset($_POST['comments']))
           $comments = $_POST['comments'];
           else $your_name = '[no_value]';
       if(isset($_POST['gender']))
           $gender = $_POST['gender'];
           else $your_name = '[no_value]';
       if(isset($_POST['beverage']))
           $beverage = $_POST['beverage'];
           else $your_name = '[no_value]';
       if(isset($_POST['spam']))
           $spam = $_POST['spam'];
           else $your_name = '[no_value]';
       echo 'The submitted contents are as follows:<br>';
       echo 'Field name: your_name       Field Value: '.htmlspecialchars($your_name).'<br>';
       echo 'Field name: comments       Field Value: '.htmlspecialchars($comments).'<br>';
       echo 'Field name: gender       Field Value: '.htmlspecialchars($gender).'<br>';
       echo 'Field name: beverage       Field Value: '.htmlspecialchars($beverage).'<br>';
       echo 'Field name: spam       Field Value: '.htmlspecialchars($spam).'<br>';
   }
   else{
?>
   <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
       <table>
           <tr valign="middle">
               <td valign="right">Your name</td>
               <td><input name="your_name" type="text" size="20" /></td>
           </tr>
           <tr valign="top">
               <td valign="right">Comments</td>
               <td><input name="comments" row="text" size="20" /></td>
           </tr>
           <tr valign="middle">
               <td valign="right">Gender</td>
               <td><select name="gender" rows="3" cols="40">
                       <option value="">[select one]</option>
                       <option value="male">male</option>
                       <option value="female">female</option>
                       <option value="none of your business">none of your business</option>
                   </select>
               </td>
           </tr>
           <tr valign="top">
               <td valign="right">Beverage Preference</td>
               <td>
                   <input type="radio" name="beverage" value="coffee" />Coffee &nbsp;&nbsp;
                   <input type="radio" name="beverage" value="tea" />Tea &nbsp;&nbsp;
                   <input type="radio" name="beverage" value="beer" />Beer &nbsp;&nbsp;
               </td>
           </tr>
           <tr valign="top">
               <td valign="right">&nbsp;</td>
               <td><input type="checkbox" name="spam" value="wants_spam" />Please spam me mercilessly</td>
           </tr>
           <tr valign="top">
               <td colspan="2" align="center">
                   <input type="submit" value="submit" />
                   <input type="hidden" name = "hidden_field" value="_submitted" />
               </td>
           </tr>
       </table>      
   </form>
<?php } ?>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote