0

ユーザーの選択に基づいて、社内のさまざまな部門にメールを送信するドロップダウン メニューがあります。メニューの最初のオプションである「部門の選択」に値を持たないようにする必要があるため、ユーザーはそれを選択できず、部門を選択する必要があります。

これが私の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>

大変助かりました!ありがとう

4

2 に答える 2

1

($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>
于 2013-10-17T22:27:56.527 に答える
0

属性<option>が空の を出力する必要があります。valueたとえば、次の HTML を使用できます ( <option4 行目の に注意してください)。

<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>
于 2013-10-17T22:10:46.160 に答える