<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<?php

$HTML_TAG = array(

"firstname" =>
"&lt;input type=&quot;text&quot; name=&quot;firstname&quot;&gt;",

"school" =>
"&lt;input type=&quot;text&quot; name=&quot;school&quot; value=&quot;Niagara College&quot;&gt;",

"secret" =>
"&lt;input type=&quot;password&quot; name=&quot;secret&quot;&gt;",

"email" =>
"&lt;input type=&quot;checkbox&quot; name=&quot;email&quot;&gt;",

"snailmail" =>
"&lt;input type=&quot;checkbox&quot; name=&quot;snailmail&quot; value=&quot;yes&quot;&gt;",

"prevaddress" =>
"&lt;input type=&quot;checkbox&quot; name=&quot;prevaddress&quot; checked=&quot;checked&quot;&gt;",

"question10" =>
"&lt;input type=&quot;radio&quot; name=&quot;question10&quot; value=&quot;a&quot; checked=&quot;checked&quot;&gt;\n" .
"&lt;input type=&quot;radio&quot; name=&quot;question10&quot; value=&quot;b&quot;&gt;\n" .
"&lt;input type=&quot;radio&quot; name=&quot;question10&quot; value=&quot;c&quot;&gt;\n" .
"&lt;input type=&quot;radio&quot; name=&quot;question10&quot; value=&quot;d&quot;&gt;\n" .
"&lt;input type=&quot;radio&quot; name=&quot;question10&quot; value=&quot;e&quot;&gt;",

"question11" =>
"&lt;input type=&quot;radio&quot; name=&quot;question11&quot; value=&quot;yes&quot; checked=&quot;checked&quot;&gt;\n" .
"&lt;input type=&quot;radio&quot; name=&quot;question11&quot; value=&quot;no&quot;&gt;",

"flavour" =>
"&lt;select name=&quot;flavour&quot;&gt;\n" .
"  &lt;option value=&quot;0&quot; selected=&quot;selected&quot;&gt;--Choose a flavour--&lt;/option&gt;\n" .
"  &lt;option value=&quot;100&quot;&gt;Vanilla&lt;/option&gt;\n" .
"  &lt;option value=&quot;102&quot;&gt;Strawberry&lt;/option&gt;\n" .
"  &lt;option value=&quot;106&quot;&gt;Rum and Raisin&lt;/option&gt;\n" .
"  &lt;option value=&quot;114&quot;&gt;Peach and Orange&lt;/option&gt;\n" .
"&lt;/select&gt;",

"comments" =>
"&lt;textarea name=&quot;comments&quot; rows=&quot;10&quot; cols=&quot;78&quot;&gt;\n" .
"&lt;/textarea&gt;",

"source" =>
"&lt;input type=&quot;hidden&quot; name=&quot;source&quot; value=&quot;http://somesite.tld/&quot;&gt;"

);

if ( $_POST )
{

function
printtag( $name, $html )
{
    global $encoded ;

    $cooked = $_POST[ $name ] ;
    $raw = $encoded[ $name ] ;
    $cookedlength = strlen( $cooked );
    $rawlength = strlen( $raw ) ;
?>
<h2><?php echo( $name ) ; ?></h2>
<p>Form tag:</p>
<pre><?php echo( $html ) ; ?></pre>

<p>Encoded value (<?php echo( $rawlength ); ?> bytes):</p>
<?php
    if ( isset( $raw ) )
    {
        if ( $rawlength > 0 )
        {
?>
<pre><?php echo( $raw ) ; ?></pre>
<?php
        }
        else
        {
?>
<p><em>Empty</em></p>
<?php
        }
    }
    else
    {
?>
<p><em>Undefined</em></p>
<?php
    }
?>
<hr>
<p>Decoded value (<?php echo( $cookedlength ); ?> bytes):</p>
<?php
    if ( isset( $cooked ) )
    {
        if ( $cookedlength > 0 )
        {
?>
<pre><?php echo( $cooked ) ; ?></pre>
<?php
        }
        else
        {
?>
<p><em>Empty</em></p>
<?php
        }
    }
    else
    {
?>
<p><em>Undefined</em></p>
<?php
    }
?>
<hr>
<?php
}
?>

<title>CTEC1906 - Form Results</title>
</head>
<body>
<h1>Form Content</h1>

<h2>Raw Form Data</h2>

<pre><?php $rawdata = file_get_contents( "php://input" ) ;
echo( $rawdata ) ?></pre>

<p>Content length: <?php printf( "%d bytes",
 $_SERVER[ 'CONTENT_LENGTH' ] ) ; ?></p>

<?php

$kvps = preg_split( "/\&/", $rawdata ) ;
$encoded = array( ) ;

foreach ( $kvps as $kv )
{
    list( $key, $value ) = preg_split( "/=/", $kv ) ;
    $encoded[ $key ] = $value ;
}

$keys = array_keys( $HTML_TAG ) ;

sort( $keys ) ;

foreach ( $keys as $key )
{
    printtag( $key, $HTML_TAG[ $key ] ) ;
}

?>
<p><a href="/courses/ctec1906/">Back to CTEC1906</a></p>
</body>
</html>

<?php
}
else    /* ! POST */
{
?>
  <title>CTEC1906 - HTML Input Tags</title>

  <!-- CSS and background image copied from                             -->
  <!-- http://www.w3.org/MarkUp/Guide/                                  -->
  <!-- Dave Raggett [dsr@w3.org]                                        -->
  <!-- Copyright (C)1994-2003 W3C(R) (MIT, ERCIM, Keio),                -->
  <!-- All Rights Reserved. W3C liability, trademark, document use and  -->
  <!-- software licensing rules apply. Your interactions with this site -->
  <!-- are in accordance with our public and Member privacy statements. -->

  <style>
  body { 
    background-color: white ;
    color: black ;
    background: url( 'images/book.jpg' ) ;
    background-repeat: repeat-y ;
    margin-left: 10% ;  
    margin-right: 10% ; 
    font-family: "Arial", "Helvetica", sans-serif ;
  }
  pre {
     color: green ;
     font-weight: bold ;
     white-space: pre ;
     font-family: "Courier New", "Courier", monospace ;
  }
  .navbar {
      text-align: center ;
  }
  .formtag {
    font-family: "Arial", "Helvetica", sans-serif ;
        color: #0000FF ;
        background-color: #FFFF00 ;
  }
  #title {
    text-align: center ;
  }
  </style>

</head>

<body>
<h1 id="title">HTML5 Forms and Form Processing in PHP</h1>

<p>A form:</p>
<pre>
&lt;form method=&quot;post&quot; action=&quot;<?php
 echo( $_SERVER[ 'PHP_SELF' ] ); ?>&quot;&gt;
</pre>

<form method="post" action="<?php echo( $_SERVER[ 'PHP_SELF' ] ); ?>">

<p>A text field (edit box) called "firstname", initially blank:</p>

<pre><?php echo( $HTML_TAG[ 'firstname' ] ) ; ?></pre>

<p class="formtag">Enter your first name: <input type="text"
 name="firstname"></p>

<p>A text field with a default (already filled-in value):</p>

<pre><?php echo( $HTML_TAG[ 'school' ] ) ; ?></pre>

<p class="formtag">Your school is <input type="text" name=
 "school" value="Niagara College"></p>

<p>A password field (edit box) called "secret", which shows asterisks
as you type, for privacy:</p>

<pre><?php echo( $HTML_TAG[ 'secret' ] ) ; ?></pre>

<p class="formtag">What is your secret password? <input type=
 "password" name="secret"></p>

<p>A checkbox, which gets sent as "email=on" when it is checked:</p>

<pre><?php echo( $HTML_TAG[ 'email' ] ) ; ?></pre>

<p class="formtag"><input type="checkbox" name="email"> Send
 confirmation by mail</p>

<p>A checkbox, which gets sent as "snailmail=yes" when it is checked:</p>

<pre><?php echo( $HTML_TAG[ 'snailmail' ] ) ; ?></pre>

<p class="formtag"><input type="checkbox" name="snailmail" value=
 "yes"> Send confirmation by Canada Post</p>

<p>Checkboxes are not sent when they are unchecked.</p>

<p>A checkbox that is already checked:</p>

<pre><?php echo( $HTML_TAG[ 'prevaddress' ] ) ; ?></pre>

<p class="formtag"><input type="checkbox" name=
 "prevaddress" checked="checked"> Mailing address is
 same as residence</p>

<p>A group of radio buttons; they all have the same name, but
different values.  One should be initially marked "checked":</p>

<pre><?php echo( $HTML_TAG[ 'question10' ] ) ; ?></pre>

<p class="formtag">Question 10.</p>

<p class="formtag">Rate your satisfaction with this product:</p>

<ol type="a" class="formtag">

  <li class="formtag"><input type="radio" name="question10" value=
    "a" checked="checked">Very satisfied</li>

  <li class="formtag"><input type="radio" name="question10" value=
     "b">Satisfied</li>

  <li class="formtag"><input type="radio" name="question10" value=
     "c">Neutral</li>

  <li class="formtag"><input type="radio" name="question10" value=
     "d">Dissatisfied</li>

  <li class="formtag"><input type="radio" name="question10" value=
     "e">Very dissatisfied</li>

</ol>

<p>A second group of radio buttons:</p>

<pre><?php echo( $HTML_TAG[ 'question11' ] ) ; ?></pre>

<p class="formtag">Question 11.</p>

<p class="formtag">Have you ever been to Disney World? 
<input type="radio" name="question11" value="yes" checked="checked"> Yes
<input type="radio" name="question11" value="no"> No
</p>

<p>A pop-up menu:</p>

<pre><?php echo( $HTML_TAG[ 'flavour' ] ) ; ?></pre>

<p class="formtag">Your favourite flavor of ice cream?
<select name="flavour">
  <option value="0" selected="selected">--Choose a flavour--</option>
  <option value="100">Vanilla</option>
  <option value="102">Strawberry</option>
  <option value="106">Rum and Raisin</option>
  <option value="114">Peach and Orange</option>
</select>
</p>

<p>A multi-line text field, so that the user can type in a 
paragraph.&nbsp; The number rows and columns can be specified:</p>

<pre><?php echo( $HTML_TAG[ 'comments' ] ) ; ?></pre>

<p class="formtag">Comments<br>
<textarea name="comments" rows="10" cols="78">
</textarea>
</p>

<p>A "hidden" input field, that is not shown in the browser
window, but can be used to send additional information
to a CGI app:</p>

<pre><?php echo( $HTML_TAG[ 'source' ] ) ; ?></pre>

<input type="hidden" name="source" value="http://somesite.tld/">

<p>A submit button, that sends the encoded form data to the
CGI app.  The "name" attribute is optional; the "value"
attribute is the text shown on the button itself:</p>

<pre>
&lt;input type=&quot;submit&quot; value=&quot;Go!&quot;&gt;
</pre>

<p class="formtag"><input type="submit" value="Go!"></p>

<p>A reset button, which sets input fields to "default"
values, or clears them if there are no default values.
No information is sent to the server or CGI app.</p>

<pre>
&lt;input type=&quot;reset&quot; value=&quot;Start Over&quot;&gt;
</pre>

<p class="formtag"><input type="reset" value="Start Over"></p>

<p>End the form:</p>

<pre>
&lt;/form&gt;
</pre>

</form>

<hr>

<p class="navbar"><a href="../">Back to CTEC1906</a></p>

</body>
</html>
<?php
} /* end else ! POST */
?>