KateG
KateG

Reputation: 107

Form Error Message Not Working

Created a form in a XHTML document for a 'Contact Us' page. There is an error message if starred forms are not filled in/selected, and it works for every single input box except one.

This is the drop down form the error message doesn't work on:

<div class='container'>
<label for='destemail' >Select department you're trying to reach: 
<font style="color:#f93;  margin-left:-2px; ">*</font></label></br>

<select name="destemail" id="destemail">
<?php foreach ($emailAddresses as $name => $email) { ?>  

<option value="<?php echo htmlspecialchars($destemail); ?>">
<?php echo htmlspecialchars($name) ; ?></option> 
<?php } ?>
</select>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>

Every time I test the form, regardless of whether a department is selected here or not, the error message comes up saying "Please select a department." I need this only to show up IF a department is not actually selected.

Here is the PHP:

if(empty($_POST['destemail']))
{
    $this->add_error("Please select a department.");
    $ret = false;
    }

Here is the Javascript:

<script type='text/javascript'>
// <![CDATA[

var frmvalidator  = new Validator("contactus");
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();

frmvalidator.addValidation("destemail","req","Please select a department.");

// ]]>

</script>

Because I am NOT by any means a web developer, just a designer, and I got this form working thus far because of tutorials and the help of strangers on Stack Overflow, I'm confused by why my error message keeps showing up regardless of whether a department is selected or not. Help is appreciated. Thanks!

Upvotes: 0

Views: 133

Answers (1)

spacebean
spacebean

Reputation: 1554

I think it's because of this bit, your value for the email is a variable that's not defined so it will go through empty: <option value="<?php echo htmlspecialchars($destemail); ?>">

Just change to

<select name="destemail" id="destemail">
<?php foreach ($emailAddresses as $name => $email) { ?>  

<option value="<?php echo htmlspecialchars($email); ?>">
<?php echo htmlspecialchars($name) ; ?></option> 
<?php } ?>
</select>

Upvotes: 1

Related Questions