2012-01-20 145 views
0

我问这个问题,关于我的朋友,所以我没有代码示例要在这里发布。希望我很清楚,有人可以提供帮助。Jquery + PHP表单 - 基于复选框选择的多个收件人

因此,他有一个简单的接触形式,除了它具有多个复选框,用户可以选择将他们的请求发送给多个收件人......像这样......


X我想知道飞行学校
X我想成为一名教师
X我希望有人与我联系你度

名称
电子邮件
条评论


所以在此基础上的复选框被选中,让他们接受用户的意见和利益,应该添加收件人的电子邮件功能。

表单由jquery验证,并使用$ .ajax函数将名称,电子邮件和注释字段发布到process.php中......我们验证至少选中了一个复选框,但是,我们还无法弄清楚如何将其布尔值传递给process.php,并将相关的电子邮件地址添加到mail()函数中。

我意识到这是半隐瞒的,没有发布我们的代码,但我现在无法访问它......并且我一直在搜索谷歌约30分钟,试图找到合适的东西。任何帮助,将不胜感激。谢谢。

回答

0

你可以简单地检查,如果你得到的价值是真还是假:

基本思想:

if(checkbox-1-ischecked) 
    //send email to first recipent 
end if 
if(checkbox-2-ischecked) 
    //send email to 2nd recipent 
end if 
if(checkbox-3-ischecked) 
    //send email to 3rd recipent 
end if 
if(checkbox-4-ischecked) 
    //send email to 4th recipent 
end if 

0

名称的元素,像这样的数组:

<input type="checkbox" name="mybox[]" value="[email protected]">Foo</input> 
<input type="checkbox" name="mybox[]" value="[email protected]">Bar</input> 
<input type="checkbox" name="mybox[]" value="[email protected]">Hello</input> 
<input type="checkbox" name="mybox[]" value="[email protected]">World</input> 

将表单发布到您的PHP后,$_POST['mybox']将是一个数组检查框的值。

在你的PHP

if(isset($_POST['my_box'])) 
{ 
    $subject = "sub"; 
    $body = "body"; 
    if (is_array($_POST['mybox'])) 
    { 
     //multiple items were selected. 
     $to = implode(',',$_POST['my_box']); 

     mail($to,$subject,$body); 
    } 
    else //only one item was selected 
    { 
     echo $_POST['my_box']; 
     $to = $_POST['my_box']; 
     mail($to,$subject,$body); 
    } 
} 
else 
    //none were selected 
+0

刚一说明,我知道这已经超出了问题的范围和答案,但你必须确保你所有消毒后的变量,以防止让你的表单被垃圾邮件发送者劫持。 – megaSteve4

0

这似乎回答您复选框查询。 (http://stackoverflow.com/questions/908708/how-to-pass-multiple-checkboxes-using-jquery-ajax-post)

基本上来说,它会发布一个数组回到你的PHP脚本然后可以解析和取决于被勾选/变量传回的内容,然后可以将更多电子邮件地址附加到邮件功能的“到”部分。

对于一个简单的implimentation,你可以保持你的三个复选框不在数组中,而ajax将它们单独发回。 HTML

<input type='checkbox' name='flight' value='1' id='flight' /> 
<input type='checkbox' name='teacher' value='1' id='teacher' /> 

然后,只需在服务器上通过PHP

$to=""; 
if($_POST['teacher'] == 1) {$to = $to."[email protected],"};//append email 
if($_POST['flight'] == 1) {$to = $to."[email protected],"};//append email 
$to = rtrim($to, ","); //remove trailing comma 

注意与所有网络邮寄脚本确保您消毒所有瓦尔防止滥发垃圾!

0

您可以简单地为所有复选框分配相同的名称,这实际上会导致复选框数组。

<form name="someform" onsubmit="return validate(this)" action="process.php" method="post"> 
    <input type="checkbox" name="names[]" value="Saji">Saji 
    <input type="checkbox" name="names[]" value="Muhaimin">Muhaimin 
    <input type="checkbox" name="names[]" value='Muhsin'>Muhsin 
    <input type="submit" value="Submit"> 
    <input type="reset" value="Reset"> 
</form> 

在process.php,你可以有─

$name_val=$_POST['names']; 
    foreach($name_val as $values){ 
     //Here $values will contain only the values of the checkboxes you had selected. 
     echo $values."<br />"; 
    }