如何使我的下拉菜单中的第一个选项没有价值?

时间:2013-10-17 21:59:08

标签: php drop-down-menu

我有一个下拉菜单,可根据用户选择将电子邮件发送到公司的不同部门。我需要菜单中的第一个选项“选择部门”,没有任何价值,因此用户无法选择它,他们必须选择一个部门。

这是我的PHP:

// config
$emailAddresses = array(

'Select Department'=>'',
'Service Department'=>'blahblah1@gmail.com',
'Sales Department'=>'blahblah2@gmail.com',
'Parts Department'=>'blahblah3@gmail.com',
'Customer Service Department'=>'blahblah4@gmail.com',
'Bids Department'=>'blahblah5@gmail.com'

 // etc etc
 );

这是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>

非常感谢!感谢

2 个答案:

答案 0 :(得分:1)

您正在使用($ emailAddresses为$ name =&gt; $ email),因此$ name既是标签又是您选择中的值。这是你的问题....我认为你的意思是使用$ email作为值,$ name作为选项标签(用户可见的值)。如果这是正确的,那么当前问题中的数组应该可以处理这些更改。

$emailAddresses = array(

   'Select Department'=>'',
   'Service Department'=>'blahblah1@gmail.com',
   'Sales Department'=>'blahblah2@gmail.com',
   'Parts Department'=>'blahblah3@gmail.com',
   'Customer Service Department'=>'blahblah4@gmail.com',
   'Bids Department'=>'blahblah5@gmail.com'

    // 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>

将它放在一个文件中并运行它以查看一切是如何工作的并且应该设置。你允许destemail有一个空白值,所以允许它发布一个...我认为这可能是你看到的问题。测试这个页面。使用“选择部门”提交表单,然后提交其他一些选项以查看这是否是您想要的行为。

<?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'=>'Service@gmail.com',
   'Sales Department'=>'Sales@gmail.com',
   'Parts Department'=>'Parts@gmail.com',
   'Customer Service Department'=>'CustomerService@gmail.com',
   'Bids Department'=>'Bids@gmail.com' 
); 

?>

<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>

答案 1 :(得分:0)

您应该输出一个<option>属性为value的{​​{1}}。例如,您可以使用此HTML(请注意第四行的<option):

<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>