0

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!

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

This fixed the error message problem, but now everytime I fill out the form and press "Send", it says I have not chosen a department because of this PHP at the top of my document. How is this affecting it not being able to send? if (isset($_POST['name'])) { if (!isset($emailAddresses[$_POST['destemail']])) { exit("Sorry, you have not selected a department."); } I tried taking this code out of the equation all together, but then nothing shows up on the page at all. @spacebean
I think you just want to say if (!isset($_POST['destemail']), right? They way it's written it would be looking for the email (as the key) in the emailAddresses array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.