Hey guys, I am trying to make a form with validation that stores the previously entered information. Below is an example. I want it to store the brand selection as it does with the domain_name which is text. But when I click submit, it doesn't error out but just shows blank selection, if I ht submit again it will error because blank was selected before. How can I do this, remeber, it works fine with the text filed domain_name. It retains what is entered no matter how many times Submit is clicked. Thanks a ton!
<?php
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['brand']=='')
$arrErrors['brand'] = 'Please select a Brand.';
if ($_POST['vendor']=='')
$arrErrors['vendor'] = 'A valid vendor is required.';
if ($_POST['domain_name']=='')
$arrErrors['domain_name'] = 'Please provide a domain name.';
if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here.
} else {
// The error array had something in it. There was an error.
// Start adding error text to an error string.
$strError = '<div class=""><p><img src="/vbulletin/dev/CSR/images/icon_err.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
// Get each error and add it to the error string
// as a list item.
foreach ($arrErrors as $error) {
$strError .= "<li>$error</li>";
}
$strError .= '</ul></div>';
}
}
?>
<style>
label {
width: 100px;
text-align: right;
float: left;
}
.formerror {
border: 1px solid red;
background-color : #FFCCCC;
width: auto;
padding: 5px 0;
}
.errortext {
padding-left: 40px;
font: bold smaller sans-serif;
}
</style>
<?php echo $strError; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!--
For every form field, we do the following...
Check to see if there's an error message for this form field. If there is,
add the formerror class to the surrounding paragraph block. The formerror
class contains the highlighted box.
Insert the contents of what the user submitted bak into the form field.
Check again to see if this field has an error message. If it does, show
the error icon and the error message next to the field.
-->
<label for="name">Brand :</label>
<select name="brand" id="brand" value="<?php echo $_POST['brand'] ?>">
<option></option>
<option>brand1</option>
<option>brand2</option>
</select>
<?php if (!empty($arrErrors['brand'])) echo '<img src="/vbulletin/dev/CSR/images/icon_err.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['brand'].'</span>'; ?>
</p>
<p<?php if (!empty($arrErrors['vendor'])) echo ' class=""'; ?>>
<label for="vendor">Vendor:</label>
<select name="vendor" id="vendor" value="<?php echo $_POST['vendor'] ?>">
<option></option>
<option>vendor1</option>
<option>vendor2</option>
</select>
<?php if (!empty($arrErrors['vendor'])) echo '<img src="/vbulletin/dev/CSR/images/icon_err.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['vendor'].'</span>'; ?>
</p>
<p<?php if (!empty($arrErrors['domain_name'])) echo ' class=""'; ?>>
<label for="phone">Domain Name:</label>
<input name="domain_name" type="text" id="domain_name" value="<?php echo $_POST['domain_name'] ?>">
<?php if (!empty($arrErrors['domain_name'])) echo '<img src="/vbulletin/dev/CSR/images/icon_err.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['domain_name'].'</span>'; ?>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>