使用复选框通过电子邮件发送表单。
某些复选框有一个可选字段来输入文本。喜欢“其他和更多信息”
如何将人们输入的文本附加到此选项的复选框中。
这是一个小组。
<form action="sendemail.php" method="POST" name="Contact Form">
<div><input type="checkbox" class="checkbox" name="check[]" value="Word of Mouth?">Word of Mouth?</div>
<div><input type="checkbox" class="checkbox" name="check[]" value="Have you seen our Sign? Where?">Have you seen our Sign? Where?<input type="text" style="float:right; width:150px;" name="sign" /></div>
<div><input type="checkbox" class="checkbox" name="check[]" value="Other.">Other. <input type="text" style="float:right; width:300px;" name="other" /></div>
<p><input type="submit" value="Submit" name="submit"></p>
我的sendmail.php就像:
<?php
if(isset($_POST['submit'])) {
$to = "myemail@mysite.com";
$subject = "Contact Form";
$firstname_field = $_POST['firstname'];
$lastname_field = $_POST['lastname'];
foreach($_POST['check'] as $value) {
$check_msg_0 .= " $value\n";
}
$body = "From: $firstname_field $lastname_field\n Learned about us from: \n $check_msg_0\n \n ";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "Nope didn't work!";
}
?>
更新 - 尝试过推荐但没有效果。我无法将值和文本字段放在一起。
试过这个,但仍然没有结果。
<form action="sendemail.php" method="POST" name="Contact Form">
<div><input type="checkbox" class="checkbox" name="check[]" value="Word of Mouth?">Word of Mouth?</div>
<div><input type="checkbox" class="checkbox" name="check[]" value="Other.">Other. <input type="text" style="float:right; width:300px;" name="otherwhere" /></div>
<p><input type="submit" value="Submit" name="submit"></p>
if (count($_POST['check']) > 0) {
foreach ($_POST['check'] as $value) {
if (isset($_POST['check']) && $_POST['check'] == 'Other') {
$check_msg_0 .= $value.": ".$_POST['otherwhere']."<br>";
}
else {
$check_msg_0 .= $value."<br>";
}
}
}
更新2 - 让它发挥作用。将我的文本域名称更改为“otherwhereTXT”并执行以下操作:
if (count($_POST['check']) > 0) {
foreach ($_POST['check'] as $value) {
if ($value == 'other') {
$check_msg_0 .= "Other: ".$_POST['otherwhereTXT']."<br>";
} else {
$check_msg_0 .= $value."<br>";
}
}
}
答案 0 :(得分:0)
你会用......
$body = "From: $firstname_field $lastname_field\n Learned about us from: \n";
if (count($_POST['check'] > 0 {
foreach ($_POST['check'] as $value) {
$body .= $value."\n";
}
}
if (strlen($_POST['other']) > 0) {
$body .= "\n\nOther: ".$_POST['other'];
}
这将遍历每个选中的值(复选框仅在$ _POST中显示,如果已经选中)并将它们添加到正文消息中。