Starting at line 55
<?php
//Array to hold all of the province choices
$PROVINCES = array( "--" => "---Please Select Provinces---",
"nf"=>"Newfoundland",
"pe"=>"PrinceEdwardIsland",
"nb"=>"New Brunswick",
"ns"=>"Nova Scotia",
"qc"=>"Quebec",
"on"=>"Ontario",
"mb"=>"Manitoba",
"sk"=>"Saskatchewan",
"ab"=>"Alberta",
"bc"=>"British Columbia",
"nt"=>"Northwest Territories");
//array for all the possible error messages
$errorMessages = array ('Username cannot be empty', 'Email cannot be empty', 'Year cannot be empty',
'Status must be selected', 'Location must be selected', 'Username must contain at least 5 characters'
);
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$dispWelcomeMsg = TRUE;
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($_POST['uname']) == TRUE){
$errorMessages[0];
}
if (empty($_POST['email']) == TRUE){
$errorMessages[1];
}
if (empty($_POST['year']) == TRUE){
$errorMessages[2];
}
if (empty($_POST['status']) == TRUE){
$errorMessages[3];
}
if (empty($_POST['location']) == TRUE){
$errorMessages[4];
}
if (count($errorMessages) > 0) {
$dispErrorMsgs = TRUE;
} else {
foreach ($_POST as $key => $val){
$successMessages[$key] = $val;
}
$dispSuccessMsg = TRUE;
}
}
if (strlen($_REQUEST["uname"]) < 5) {
echo "uname:" . $_REQUEST["uname"]; //check to see if username is at least 5 characters long
} else
$errorMessages[5];
array_walk_recursive($_POST, function (&$val) //Trim all the white space from strings
{
$val = trim($val);
});
?>
<html>
<head>
<title>Comp10065 - Lab 2: Simple Form Validation</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
font: 80%/1.5 Arial, Helvetica, sans-serif;
color: #111;
background-color: white;
}
h2 {
margin: 0px;
padding: 10px;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 200%;
font-weight: normal;
color: #FFF;
background-color: #CCC;
border-bottom: #BBB 2px solid;
}
h1 {
color:#006600
}
p#copyright {
margin: 20px 10px;
font-size: 90%;
color: #999;
}
div.form-container {
margin: 10px;
padding: 5px;
background-color: #FFF;
border: #EEE 1px solid;
}
p.legend {
margin-bottom: 1em;
}
p.legend em {
color: #C00;
font-style: normal;
}
div.errors {
margin: 0 0 10px 0;
padding: 5px 10px;
border: #FC6 1px solid;
background-color: #FFC;
}
div.errors p {
margin: 0;
}
div.errors p em {
color: #C00;
font-style: normal;
font-weight: bold;
}
div.success {
margin: 0 0 10px 0;
padding: 5px 10px;
border: #FC6 1px solid;
background-color: #FFC;
}
div.success p {
margin: 0;
color:#006633
}
div.success li {
list-style-type: none;
}
div.form-container form p {
margin: 0;
}
div.form-container form p.note {
margin-left: 170px;
font-size: 90%;
color: #333;
}
div.form-container form fieldset {
margin: 10px 0;
padding: 10px;
border: #DDD 1px solid;
}
div.form-container form legend {
font-weight: bold;
color: #666;
}
div.form-container form fieldset div {
padding: 0.25em 0;
}
div.form-container label, div.form-container span.label {
margin-right: 10px;
padding-right: 10px;
width: 150px;
display: block;
float: left;
text-align: right;
position: relative;
}
div.form-container label.error, div.form-container span.error {
color: #000;
}
div.form-container select.error, div.form-container div.error {
background-color: #FFC;
}
div.form-container label em, div.form-container span.label em {
position: absolute;
right: 0;
font-size: 120%;
font-style: normal;
color: #C00;
}
div.form-container input.error {
border-color: #C00;
background-color: #FFC;
}
div.form-container input:focus, div.form-container input.error:focus, div.form-container textarea:focus {
background-color: #FFC;
border-color: #FC6;
}
div.form-container div.controlset {
margin: 3px
}
div.form-container div.controlset label, div.form-container div.controlset input {
display: inline;
float: none;
}
div.form-container div.controlset div {
margin-left: 170px;
}
div.form-container div.buttonrow {
padding-left: 400px;
border: 1px solid #CCCCCC
}
div#wrapper {
width: 700px;
margin-left: auto;
margin-right: auto
}
pre {
color:black;
font-size: 10pt;
font-weight: bold;
margin-left: 30px
}
p#welcomeMsg {
font-size:larger;
color:green;
font-style:italic;
font-weight: bold
}
table#submissionTable {
border-collapse: collapse
}
table#submissionTable tr td, table#submissionTable tr th {
vertical-align:top;
font-size: smaller;
border: 1px solid #CCC;
padding: 3px
}
</style>
</head>
<body>
<div id="wrapper">
<h1> Form Validation Lab !!!!!</h1>
<div class="form-container">
<? if ($dispWelcomeMsg): ?>
<p id="welcomeMsg">Please fill in the form........</p>
<? endif; ?>
<? if ($dispErrorMsgs): ?>
<div class="errors">
<p><em>Oops... the following errors were encountered:</em></p>
<ul>
<? foreach($errorMessages as $index => $errorMessage): ?>
<li>
<?= $errorMessage ?>
</li>
<? endforeach; ?>
</ul>
<p>Please correct the errors and re-submit this form.</p>
</div>
<? endif; ?>
<? if ($dispSuccessMsg) { ?>
<div class="success">
<p>Thank you for your submission. You entered the following values:</p>
<table id="submissionTable">
<? foreach($successMessages as $index => $successMessage): ?>
<tr>
<th><?= $index ?></th>
<td><?= $successMessage ?></td>
</tr>
<? endforeach; ?>
</table>
</div>
<? } ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="hidden" name="act" value="post">
<div class="buttonrow">
<input type="submit" value="Submit This Form " class="button" />
<a href="<?= $_SERVER['PHP_SELF'] ?>" >Start Again</a> </div>
<p class="legend"><strong>Note:</strong> Required fields are marked with an asterisk (<em>*</em>)</p>
<fieldset>
<legend>User Details</legend>
<div>
<label for="uname" >User Name <em>*</em></label>
<input id="uname" type="text" name="uname" value="<?= $_POST['uname']; ?>" />
(must be greater than 5 chars)</div>
<div>
<label for="email">Email Address </label>
<input id="email" type="text" name="email" value="" />
</div>
</fieldset>
<fieldset>
<legend>Submission</legend>
<div>
<label for="year">Year (YYYY) <em>*</em></label>
<input id="year" type="text" name="year"
value="" size="4" maxlength="4" />
(4 digit number)</div>
<div>
<label for="date">Month (MM)</label>
<input id="date" type="text" name="month" value="" size="4" maxlength="2" />
(number ranging from 1-12)</div>
</fieldset>
<fieldset>
<legend>Preferences</legend>
<div>
<label for="type" >Province (Multiple Select) <em>*</em></label>
<select name = "province[]" multiple size = "12" id="type" >
<? foreach($PROVINCES as $shortName => $longName): ?>
<option value="<?= $shortName ?>" >
<?= $longName ?>
</option>
<? endforeach; ?>
</select>
</div>
<div class= 'controlset' > <span class="label">Status (Mult Select)<em>*</em></span>
<input name="status[]" id="approved" value="approved" type="checkbox" />
<label for="approved">Approved</label>
<input name="status[]" id="pending" value="pending" type="checkbox" />
<label for="pending">Pending Application</label>
<input name="status[]" id="actives" value="actives" type="checkbox" />
<label for="actives">Active Service</label>
</div>
<div class= 'controlset' > <span class="label"> Location <em>*</em></span>
<input name="location" value="Garage" type="radio" />
<label for="radio1">Garage</label>
<input name="location" value="Attic" type="radio" />
<label for="radio2">Attic</label>
<input name="location" value="House" type="radio" />
<label for="radio3">House</label>
</div>
</fieldset>
</form>
<hr>
<p>This debug info is here to help you. You are only required to display the POST array</p>
<pre>$TPL = Array
(
[dispWelcomeMsg] => 1
)
</pre>
</div>
</div>
</body>
</html>
This post has been edited by biggmann: 30 September 2012 - 10:18 AM

New Topic/Question
Reply



MultiQuote








|