你使用($ emailAddresses爲$名=> $電子郵件):例如,你可以使用這個HTML(注意<option
第四行)您選擇的值。這是你的問題....我想你的意思是使用$ email作爲值,$ name作爲選項標籤(用戶可見的值)。如果這是正確的,那麼你的數組在當前的問題中應該適用於這些更改。
$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>
把這個文件並運行它看到的一切是如何工作的,應當成立。你允許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'=>'[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>
你可以試試這個:''=>'*選項我想沒有值*',作爲第一個數組元素? –
如果你想阻止他們選擇它,你必須使用驗證。將它設置爲沒有值不會這樣做。 – Barmar
你如何將這個數組轉換成一個選擇框?你使用框架還是一些自定義代碼?如果是後者,請發佈代碼。 –