Jump to content

Help: Error messages won't appear


jaArch

Recommended Posts

I have to get the error messages to appear...but for some reason they're not showing up if I leave the forms blank and click submit.

 

  <?
  $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'] == ''){
	$errMsg['uname'] = "Username must not be blank!!!";  
	$unameInError = true;
  }
  if (empty($_POST['year']) == TRUE){
	  $errMsg['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><?= $errMsg ?> </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
https://forums.phpfreaks.com/topic/248720-help-error-messages-wont-appear/
Share on other sites

Actually, I found the problem through trying your code -

 

Notice: Undefined variable: errMsgs in your_file line 122

 

^^^ That's not the name of the error message array you are setting values into.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.