我想让我的php表单显示哪些单选框已被选中。换句话说,当有人通过我的网站通过电子邮件联系我时,我希望它能显示他们选中了哪个单选框。我什么都试过了,就是不行!
HTML CODE SO FAR -
<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
$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
}
?>
您错误地构建了复选框输入。value
属性应该是复选框显示的"name",例如
<input type="checkbox" name="servicetype[]" value="Technical Support" id="Technical Support" />
^^^^^^^^ ^^^^^^^^^^^^^^^^^
与表单一起提交的是value
。按照目前的情况,您只需提交单词"checkbox",并且无法知道哪个复选框被选中。
你的电子邮件可以是:
$body_message = "blah blah blah. Requested services: " . implode(',' $_POST['servicetype']);