2013-10-17 58 views
0

我有一個下拉菜單,根據用戶選擇向公司的不同部門發送電子郵件。我需要菜單中的第一個選項「選擇部門」沒有任何價值,因此用戶無法選擇它,他們必須選擇一個部門。如何使我的下拉菜單中的第一個選項沒有價值?

這是我的PHP:

// config 
$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]' 

// 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> 

幫助非常感謝!謝謝

+0

你可以試試這個:''=>'*選項我想沒有值*',作爲第一個數組元素? –

+0

如果你想阻止他們選擇它,你必須使用驗證。將它設置爲沒有值不會這樣做。 – Barmar

+0

你如何將這個數組轉換成一個選擇框?你使用框架還是一些自定義代碼?如果是後者,請發佈代碼。 –

回答

1

你使用($ 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> 
+0

現在顯示錯誤代碼,但即使在選擇部門並按提交時也會顯示。這裏是我的PHP錯誤代碼: \t \t'// destemail validaions \t \t如果(空($ _ POST [ 'destemail'])){ $ 這 - > add_error(「請選擇一個部門。「); $ RET = FALSE; }'任何想法是怎麼回事@ gloomy.penguin – KateG

+0

所以...您提交表格後,你不是爲選擇('destmail接受'$ _POST'值')?你打通'$ _POST'等信息,爲這種形式(就是一切空白或只是項目)?是的形式方法設置後(而不是獲得)?是的'

'和'
之間的選擇'標籤?如果這些建議無效,我可能需要查看周圍的表單和元素以幫助排查問題。 –

0

您應該輸出一個<option>並帶有空的value屬性。從而使$ name是兩個標籤和

<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> 
相關問題