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