KateG
KateG

Reputation: 107

How do make the first option in my drop down menu have no value?

I have a drop down menu that sends emails to different departments in a company based on user selection. I need the first option in the menu, the "Select Department", to have no value, so users cannot select it, they must choose a department.

Here is my PHP:

// config
$emailAddresses = array(

'Select Department'=>'',
'Service Department'=>'[email protected]',
'Sales Department'=>'[email protected]',
'Parts Department'=>'[email protected]',
'Customer Service Department'=>'[email protected]',
'Bids Department'=>'[email protected]'

 // etc etc
 );

Here is the HTML:

<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($name); ?>">
<?php echo htmlspecialchars($name) ; ?></option>
<?php } ?>
</select>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>

Help much appreciated! Thanks

Upvotes: 0

Views: 1632

Answers (2)

gloomy.penguin
gloomy.penguin

Reputation: 5911

you're using ($emailAddresses as $name => $email) so that the $name is both the label and the value in your select. This is your problem.... I think you mean to use the $email as the value and the $name as the option label (value visible to the user). if this is correct, your array as it is in the question currently should work with those changes.

$emailAddresses = array(

   'Select Department'=>'',
   'Service Department'=>'[email protected]',
   'Sales Department'=>'[email protected]',
   'Parts Department'=>'[email protected]',
   'Customer Service Department'=>'[email protected]',
   'Bids Department'=>'[email protected]'

    // the departments become the $name in your foreach
    // the email addresses become the $email
 ); 

....

<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) { ?>  

              <!--  The reason it wouldn't work is because you had the following line: -->
              <!--    <option value="<?php echo htmlspecialchars($name); ?>">          -->

              <!--    But it needs to be this for any of that to work... -->
              <option value="<?php echo htmlspecialchars($email); ?>">
              <?php echo htmlspecialchars($name) ; ?></option> 
   <?php } ?>
  </select>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>

put this in a file and run it to see how everything works and should be set up. You are allowing destemail to have a blank value so it is allowed to post with one... I think that might be the problem you're seeing. Test this page out. Submit the form with "Select Department" and then some of the other options to see if that is your desired behavior.

<?php

// replace with the name of the current filename 
$file_name = "this_page.php";

if (!empty($_POST)) 
{
   print "<pre>".print_r($_POST,true)."</pre>"; 
}
else 
{
   print "<pre>\$_POST is empty</pre>";
}

$emailAddresses = array( 
   'Select Department'=>'',
   'Service Department'=>'[email protected]',
   'Sales Department'=>'[email protected]',
   'Parts Department'=>'[email protected]',
   'Customer Service Department'=>'[email protected]',
   'Bids Department'=>'[email protected]' 
); 

?>

<br/><br/><br/>
<form action="<?php echo $file_name ?>" method="post">
   <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($email); ?>">
                 <?php echo htmlspecialchars($name) ; ?></option> 
      <?php } ?>
     </select>
   <span id='contactus_destemail_errorloc' class='error'></span> 
   <button type="submit">Submit</button>
   </div>
</form>

Upvotes: 1

Jonathan
Jonathan

Reputation: 6732

You should output an <option> with an empty value attribute. For example, you could use this HTML (note the <option on the fourth line):

<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">
        <option value="">Select Department</option>
        <?php foreach ($emailAddresses as $name => $email) { ?>
            <option value="<?php echo htmlspecialchars($name); ?>">
                <?php echo htmlspecialchars($name) ; ?>
            </option>
        <?php } ?>
    </select>
    <span id='contactus_destemail_errorloc' class='error'></span>
</div>

Upvotes: 0

Related Questions