I am suppose to have this registration sheet that when all fields are filled out it should give me a new page with all the info I filled in except ‘*’ for password. Instead I get a errors with the original registration marking the password fields were empty when they were filed in. Here is the code
[php]
if(isset($_POST[“submitButton”])) {
processForm();
}else {
displayForm(array());
}
function validateField($fieldName, $missingFields) {
if(in_array($fieldName, $missingFields)) {
echo ‘class=“error”’;
}
}
function setValue($fieldName) {
if(isset($_POST[$fieldName])) {
echo $_POST[$fieldName];
}
}
function setChecked($fieldName, $fieldValue) {
if(isset($_POST[$fieldName]) and $_POST[$fieldName] == $fieldValue) {
echo’checked=“checked”’;
}
}
function setSelected($fieldName, $fieldValue) {
if(isset($_POST[$fieldName]) and $POST[$fieldName] == $fieldValue) {
echo’selected=“selected”’;
}
}
function processForm() {
$requiredFields = array(“firstName”,“lastName”,“password1”,“password2”,“gender”);
$missingFields = array();
foreach($requiredFields as $reequiredField) {
if( empty( $_POST[$requiredField] ) ) {
$missingFields[] = $requiredField;
}
}
if($missingFields) {
displayForm($missingFields);
} else {
displayThanks();
}
}
function displayForm($missingFields) {
?>
Membership Form
<?php if($missingFields) { ?><p class="error">There were some problems with the form you submitted.
Please complete the fields highlighted below and click Send Details to resend the form</p>
<?php } else { ?>
<p>Thanks for choosing to join. The Widget Club. To register, please
fill in your details below and click Send Details. Fields marked with an
asterisk (*) are required.</p>
<?php } ?>
<label for="lastName"<?php validateField("lastName", $missingFields) ?>>Last name *</label>
<input type="text" name="lastName" id="lastName" value="<?php setValue("lastName") ?>" /><br />
<label for="password1"<?php if($missingFields) echo'class="error"' ?>>Choose a password *</label>
<input type="password" name="password1" id="password1" value="" /><br />
<label for="password2"<?php if($missingFields) echo 'class="error"' ?>>Retype password *</label>
<input type="password" name="password2" id="password2" value="" /><br />
<label <?php validateField("gender", $missingFields) ?>>Your Gender:</label>
<label for="genderMale">Male</label>
<input type="radio" name="gender" id="genderMale" value="M"<?php setChecked("gender","M") ?> />
<label for="genderFemale">Female</label>
<input type="radio" name="gender" id="genderFemale" value="F" <?php setChecked("gender","F") ?> /><br />
<label for="favoriteWidget">What's your favorite Widget</label>
<select name="favoriteWidget" id="favoriteWdiget" size="1">
<option value="superWidger"<?php setSelected("favoriteWidget","superWidget") ?>>The SuperWidget</option>
<option value="megaWidget"<?php setSelected("favoriteWidget","megaWidget") ?>>The Mega Widget</option>
<option value="wonderWidget"<?php setSelected("favoriteWidget","wonderWidget") ?>>The Wonder Widget</option>
</select><br />
<label for="newsletter">Do you want to receive our newsletter?</label>
<input type="checkbox" name="newsletter" id="newsletter" value="yes"<?php setChecked("newsletter","yes") ?> /><br />
<label for="comments">Any comments?</label>
<textarea name="comments" id="comments" rows="4" cols="50"><?php SetValue("comments") ?></textarea>
<div style="clear:both;">
<input type="submit" name="submitButton" id="submitButton" value="Send Details" />
<input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right:20px;" />
</div>
function displayThanks() {
?>
Thank You
Thank you, your application has been received.
<?php
}
?>
[/php]