您正在使用 ($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>