将复选框值爆炸为两个单独的字符串

时间:2016-06-06 21:53:25

标签: php forms email

我的表单根据表单中选择的位置拉取数组。这种选择需要做两件事。它需要将位置名称发送给收件人,并为每个要发送的位置提取不同的电子邮件。我的问题是你需要被允许选择多个位置。我可以根据他们选择的内容来更改电子邮件,但如果有多封电子邮件则不会。

我的表单看起来像这样,但我无法收到发送到多个电子邮件地址的电子邮件。

<input type="checkbox" name="location[]" value="location1" />
<input type="checkbox" name="location[]" value="location2" />
<input type="checkbox" name="location[]" value="location3" />

<?php
$location = implode(", ",$_POST['location']);  
?>

所以我试图爆炸并将这两个分开得到两个字符串&#34; location1,location2,location3&#34;和&#34; email1,email2,email3&#34;有30个地点都有不同的电子邮件。现在我把它设置成这样。

 <input type="checkbox" name="location[]" value="location1:email1" />
<input type="checkbox" name="location[]" value="location2:email2" />
<input type="checkbox" name="location[]" value="location3:email3" />

<?php
$location = explode(":",$_POST['location']);  
$location2 = implode(", ",'$location[1]');  
?>

好的,所以在下面的评论之后我现在看:

<input type="checkbox" name="location[location1]" value="email1" />
<input type="checkbox" name="location[location2]" value="email2" />
<input type="checkbox" name="location[location3]" value="email3" />

$array = $_POST['location'];
foreach($array as $location => $email) {
echo $location;  // will write location1, location2, and location3
echo $email; // will write email1, email2, and email3

}

当print_r($ _ POST);

时,数组看起来像这样
Array
(
[otherinfo] => asdf
[g-recaptcha-response] => 
[fullname] => asdf
[email] => adsf
[phone] => asdf
[address] => asdf
[city] => asdf
[state] => asdf
[zipcode] => asfd
[workedbefore] => yes
[typedesired] => part-time
[license] => yes
[licensetype] => asdf
[location] => Array
    (
        [0] => email1, email2
    )

[select_position] => stylist
[companyname] => asdf
[companyaddress] => adsf
[companycity] => asdf
[companystate] => asdf
[positions] => adsf
[responsibilities] => asdf
[startdate] => 2016-06-07
[enddate] => 2016-06-17
[startingpay] => asdf
[endingpay] => afsd
)

知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

正如 @RiggsFolly 所提到的,更好的方法是根据电子邮件匹配位置,反之亦然,但是如果你想尝试我的建议:

这组输入:

<input type="checkbox" name="location[location1]" value="email1" />
<input type="checkbox" name="location[location2]" value="email2" />
<input type="checkbox" name="location[location3]" value="email3" />

会给你:

Array
(
    [location] => stdClass Object
        (
            [location1] => email1
            [location2] => email2
            [location3] => email3
        )
)

所以:

$array = $_POST['location'];
foreach($array as $location => $email) {
    echo $location;  // will write location1, location2, and location3
    echo $email; // will write email1, email2, and email3
}
相关问题