我有一个下拉菜单,可以根据用户选择向公司的不同部门发送电子邮件。我需要菜单中的第一个选项"选择部门"没有值,因此用户无法选择它,他们必须选择一个部门。
这是我的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>
非常感谢帮助!谢谢
您使用($emailAddresses为$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>
您应该输出一个具有空 value
属性的<option>
。例如,您可以使用以下 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>