1

我想让我的 php 表单显示已选中的单选框。换句话说,当有人通过我的网站通过电子邮件与我联系时,我希望它显示他们勾选了哪个单选框。我已经尝试了一切,但无法让它工作!

到目前为止的 HTML 代码 -

<table width="308" border="0">
      <label>
        <input type="radio" name="servicetype[]" value="checkbox" id="Technical Support" />
        Technical Support</label>

      <label>
        <input type="radio" name="servicetype[]" value="checkbox" id="Information Services" />
        Information Services</label>
  <tr>
    <td align="left">
   <input name="Submit" type="submit" id="Submit" class="contact-class2" value="Send"/>
    </td>
  </tr>
</table>
    </form>

PHP 到目前为止

<?php
 $field_name = $_POST['cf_name'];
 $field_email = $_POST['cf_email'];
 $field_message = $_POST['cf_message'];

 $mail_to = 'www.example.com';
 $subject = 'hello';

 $servicetype = join(", ", $_REQUEST["servicetype"]);
 if (isset($_POST['servicetype'])) {
    foreach ($_POST['servicetype'] as $key => $val) {
        // do what you need
    }
 }


 $body_message = ''.$field_message."\n"."\n";
 $body_message .= 'From: '.$field_name;

 $headers = 'From: '.$field_email."\r\n";
 $headers .= 'Reply-To: '.$field_email."\r\n";

 $mail_status = mail($mail_to, $subject, $body_message, $headers  );



 if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'Contact form.html';
    </script>
 <?php
 }
 else { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'Contact form.html';
    </script>
 <?php
 }
 ?>
4

1 回答 1

2

您错误地构建了复选框输入。该value属性应该是复选框的显示“名称”,例如

    <input type="checkbox" name="servicetype[]" value="Technical Support" id="Technical Support" />
                 ^^^^^^^^                              ^^^^^^^^^^^^^^^^^

它是value与表单一起提交的。就目前而言,您只需提交“复选框”这个词,并且无法知道选择了哪个复选框。

您的电子邮件只是:

$body_message = "blah blah blah. Requested services: " . implode(',' $_POST['servicetype']);
于 2013-05-14T17:27:42.137 回答