Jump to content

Simple form validation


jaArch

Recommended Posts

I'm having trouble with this form. I can't seem to figure out the rest of what is required I have already done most of it...I'm just stuck on these parts:

 

Province Select : (AutoGeneration, Multiple Select, Memory)

 

    AutoGeneration using Provinces Array: The Province Select box is generated by PHP using this array: provinces.txt Cut and paste this array into your own code.

    Multiple Selections: Note that the province select box permits multiple values to be selected. View the HTML source to see the configuration details. Also note how $_POST delivers these multiple values as an array.

    Select Box Memory: The Province select box retains the user choices after a POST. For each province selected by the user, your code will generate the "selected" attribute in the <options> tag for that province. View the HTML source of the working solution to see how this works.

 

Status CheckBoxes (Multiple Select, Memory)

 

    Multiple Selections: Note that these checkboxes have been configured in the HTML to permit multiple selections. Also note that $_POST delivers these multiple selection values in an array.

    Status Box Memory:The Status Box retains the user choices after a POST. Your code will need to generate the "checked" attribute in the <input> tag for each item selected.

    Do not generate the checkboxes with an array.

 

Location Radio button:

 

    Do not generate the radio buttons with an array.

    The Location radio button retains the user choice after a POST.

 

Validation Success Message:

 

    Where does it go - The final success message is placed within the <div class="success"></div> tags at the top of the HTML.

    Use a Loop: Generate the success message with a single array loop that reads the $_POST array and prints out the success message as an ordered list.

    Form Variables List

        Un-Ordered List - The form variables are displayed in an unordered list . CSS removes the bullet.

        Names Capitolised - The names of the form variables are capitolised ( use strtoupper() function ) .

        Handle Empty Values - Print "---None supplied---" if the variable is empty.

        Province Long Names - Print the long names of the provinces selected as comma separated string

        Status Choices - Print the status choices as a comma separated string.

 

Functions I need to use:

 

    empty() - is the string empty. Unfortunately, a "0" value counts as an empty string.

    strlen() - returns the number of characters in a string.

    is_numeric() - is the string a number

    count() - how many rows in an array

    is_array() - is the variable an array

    isset() - does the variable or member of array exist

    in_array() -does this string exist in this array

    strtoupper() - convert string to upper case.

    join() - you'll want this one too. Converts an array of strings into a string with a delimiter.

    error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); - suppresses warning messages in your code. Place at top.

    foreach ($_POST as $KEY => $VAL) {} - walk through the $_POST array (or any array)

 

My code:

 

 <!--"StAuth10065: I Adam Graham, 000136921 certify that this material is my original work. No other person's work has been used without due acknowledgement. I have not made my work available to anyone else."-->

  <?
  $PROVINCES = array(		"--" => "--- Please Select Provinces ---",
					"nf"=>"Newfoundland",
					"pe"=>"Prince Edward Island",
					"nb"=>"New Brunswick",
					"ns"=>"Nova Scotia",
					"qc"=>"Quebec",
					"on"=>"Ontario",
					"mb"=>"Manitoba",
					"sk"=>"Saskatchewan",
					"ab"=>"Alberta",
					"bc"=>"British Columbia",
					"nt"=>"Northwest Territories");
  
  if ($_SERVER['REQUEST_METHOD'] == 'GET')
  {
  $dimwelcomeMsg = TRUE;
  }
  
  if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {
  if ($_POST['uname'] == ''){
	$errMsgs['uname'] = "Username must not be blank!!!";  
	$unameInError = true;
  }
  if (empty($_POST['year']) == TRUE){
	  $errMsgs['year'] = "Year must not be blank!!";
  }
  if (count($errMsg) > 0 ):
  	$dispErrorMsg = TRUE;
	else:

	foreach ($_POST as $key => $val):
	$key = strtoupper($key);
	$successMsg[$key] = $val;
	endforeach;

	$dispSuccessMsg = TRUE;


	endif;
  }
  ?>
<html  > 

<head>
<title>Comp10065 - Simple Form Validatioon</title>


<style type="text/css">


body { margin: 0; padding: 0; font: 80%/1.5 Arial,Helvetica,sans-serif; color: #111; background-color: green; }
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: #FEF;}
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: #FEF; }
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: 430px; 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} 
</style>

</head>

<body>

<div id="wrapper">

<h1> Form Validation Lab</h1>

<div class="form-container">
<? if ($_SERVER['REQUEST_METHOD'] == 'GET') : ?> 
<? if (displayWelcomeMsg) : ?>
<p>Please fill in the form........</p>
<? endif; ?>

<? endif; ?>			

<? if ($displaySuccessMsg): ?>
<div class="success">
<p>Thank you for your submission.</p>
<ol>
<? foreach($successMsgs as $key => $successMsg): ?>
<li><?= $key ?> = <?= $successMsg ?> </li>
<? endforeach; ?>
</ol>
</div>
<? endif; ?>

<? if ($dispErrorMsg) : ?>
<div class="errors">
<p><em>Oops...the following errors were encountered</em></p>
<ul>
<? foreach ($errMsgs as $key => $errMsg): ?>
<li><?= $errMsgs ?> </li>
<? endforeach; ?>
</ul>
<p>Please correct the errors and re-submit this form.</p>
</div>
<? endif; ?>




<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>
		<? if (isset($errMsgs['uname'])) echo 'class = "errors"' ?>
            
		 <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" 
            <? if (isset($errMsgs['year'])) echo 'class = "errors"' ?>
			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 $abbreviation => $longname); ?>
                <option value = "<?=$abbreviation ?> "></option>
                <? enfgoreach; ?>
			</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" id="radio1" value="Garage" type="radio" /> <label for="radio1">Garage</label>
			<input name="location" id="radio2" value="Attic" type="radio" /> <label for="radio2">Attic</label>
			<input name="location" id="radio3" value="House" type="radio" /> <label for="radio3">House</label>

		</div>			
</fieldset>

</form>
<p>This debug info is here to help you. You are only required to display the POST array</p>
<pre><?= print_r($_POST,true) ?> </pre>
</div> 

</div> 

</body>
</html>

Link to comment
Share on other sites

I'm having trouble getting the values in the $PROVINCES array into the select box and I'm also having trouble getting the error messages to display whenever the user has left the field empty or whenever the user has entered invalid data.

Link to comment
Share on other sites

    error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); - suppresses warning messages in your code. Place at top.

 

^^^ Whoever suggested suppressing error messages, gave you some bad advice, especially when trying to learn php, develop php code, and debug php code. Hiding those types of error messages will also hide the type of problems people make when first starting out learning a programming language. Not only will your code not work, but you won't get any error messages from php alerting your to where it found detectable problems in your code.

 

As stated in your other thread for this same code/problem, I found the incorrectly named variable that is preventing your validation error messages from being displayed through trying your code and getting an undefined variable message from the code that was referencing a variable that didn't exist.

 

I guess I repeat it here, since you didn't read, understand, or follow the advice given in your existing thread -

You should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects. You will save a TON of time.

Link to comment
Share on other sites

The problem is that I just don't understand how to do this:

 

You should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects. You will save a TON of time.

 

Can you explain how I can do it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.