0

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

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

非常感谢帮助!谢谢

4

2 回答 2

1

您正在使用 ($emailAddresses as $name => $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>
于 2013-10-17T22:27:56.527 回答
0

您应该输出<option>一个空value属性。例如,你可以使用这个 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>
于 2013-10-17T22:10:46.160 回答