0

我是 stackoverflow 的新手,但在尝试了多种方法后,我无法理解这个电子邮件表单。表单工作正常,但我尝试添加 4 个可选复选框,以便客户可以勾选,以便我们知道他们感兴趣的包。我想我已经正确设置了前端:

      <p><label for="author">Full Name:</label><br />
    <input type="text" name="cf_name" class="details" tabindex="1" /></p>

<p><label for="telephone">Telephone:</label><br />
    <input type="text" name="cf_telephone" class="details" tabindex="2" /></p>

<p><label for="email">E-mail:</label><br />
         <input type="text" name="cf_email" class="details" tabindex="3" /></p>

<p><label for="package">Package Type:</label><br />
   <input type="checkbox" name="cf_package[]" class="package" value="Business English Classes" tabindex="4" /> Business English Classes<br />

        <input type="checkbox" name="cf_package[]" class="package" value="English Conversation Classes" tabindex="4" /> English Conversation Classes<br />

        <input type="checkbox" name="cf_package[]" class="package" value="Exam Preparation Classes" tabindex="4" /> Exam Preparation Classes<br />

        <input type="checkbox" name="cf_package[]" class="package" value="Writing Skills Classes" tabindex="4" /> Writing Skills Classes
</p>

        <p><label for="message">Questions/Comments:</label><br />
<textarea type="text" name="cf_message" class="questions" tabindex="8"></textarea></p>

<p style="padding-top:10px;"><input type="submit" name="submit" value="Submit" class="button" tabindex="9" /> <input type="reset" name="submit" value="Clear" class="button" tabindex="10" /></p>

我的后端脚本如下:

<code><?php
// get all values from form and remove spaces before/after values
$field_name = trim($_POST['cf_name']);
$field_telephone = trim($_POST['cf_telephone']);
$field_email = trim($_POST['cf_email']);
$field_package = trim($_POST['cf_package']);
$field_message = trim($_POST['cf_message']);

  $mail_to = 'theenglishbeehive@gmail.com';

 $subject = 'Enquiry from '.$field_name;

// check if user input own e-mail -> generate headers and send mail
if ($field_email && $field_message)
 {
 // generate body of message
   $body_message  = "From: ".$field_name."\n";
   $body_message .= "Telephone: ".$field_telephone."\n";
   $body_message .= "E-mail: ".$field_email."\n";
   $body_message .= "Package Type: ".$field_package."\n";
   $body_message .= "Questions/Comments: ".$field_message;

   $headers  = "From: ".$field_email."\n";
   $headers .= "Reply-To: ".$field_email."\n";
   $headers .= "Return-Path: ".$field_email."\n";
   $headers .= "X-Priority: 3 (Normal)\n";
   // send email
$mail_status =  mail($mail_to, $subject, $body_message, $headers);


         }

         // success ?
        if ($mail_status)
        {
        ?>
<script language="javascript" type="text/javascript">
    window.location = 'enquiry-sent.php';
</script>
       <?php
      }
    else
     {
    ?>
<script language="javascript" type="text/javascript">
    alert('Message failed. Please, send an email to    theenglishbeehive@gmail.com');
    window.location = 'index.php';
</script>
   <?php
   }
   ?>
   </code>

希望我也能在网站上正确显示。

4

1 回答 1

0

选定的“包”将作为数组而不是字符串发送到服务器,因为您使用数组表示法 ([]) 并使用复选框,人们可以选择多个包。

您可能希望将数组转换为逗号分隔的字符串并显示用户选择的包:

$field_package = implode(', ', $_POST['cf_package']); 

进而:

$body_message .= "Package Type(s): ".$field_package."\n";

如果只允许选择一个选项,您可以考虑使用单选按钮而不是复选框。

于 2013-01-27T10:34:32.533 回答