2010-06-30 66 views
0

有一个通过复选框通过电子邮件发送的表单。PHP分组表格,有些还有额外的数据

某些复选框有一个可选字段来输入文本。像“其他和更多信息”

如何将输入的文本附加到这些选项的复选框。

这是一组。

<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 = "[email protected]"; 
    $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

你可以使用...

$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露面如果他们已被选中)并将它们添加到正文消息。

+0

但我怎么会附加输入像“其他”发送电子邮件时? – pcasa 2010-06-30 19:56:47

+0

我已将它更新 – Webnet 2010-06-30 20:01:00

+0

这可以将两者分开,但需要将其作为一个整体。我用你的建议更新了我的问题。 – pcasa 2010-07-01 13:45:19

相关问题