无法使用PHP以电子邮件形式处理多个选择

时间:2013-04-22 02:41:50

标签: php html forms post sendmail

无法让我的表单从多选项中发送数据(在下面的编码中名为'country')。我已经在这里检查了几个答案,但我真的没有PHP天才能够弄清楚它是如何以及为什么它对我不起作用。当我测试它时会发生什么,表单发送最后选择的选项,就是它。

我的HTML代码如下:

<form class="well" action="contact-send.php" method="post">
            <input type="text" class="span4" name="name" placeholder="Your name" /><br />
            <input type="text" class="span4" name="email" placeholder="Your email address" /><br />
            <select class="span4" multiple="multiple" name="country[]" size="5" style="padding:4px;">
            <option value="Thailand">Thailand</option>
            <option value="Cambodia">Cambodia</option>
            <option value="Laos">Laos</option>
            <option value="Vietnam">Vietnam</option>
            <option value="India">India</option>
            <option value="Japan">Japan</option>
            <option value="Philippines">Philippines</option>
            <option value="Bhutan">Bhutan</option>
            <option value="Singapore">Singapore</option>
            <option value="Borneo">Borneo</option>
            <option value="Myanmar">Myanmar</option>
            <option value="Malaysia">Malaysia</option>
            <option value="China">China</option>
            <option value="Nepal">Nepal</option>
            <option value="Sri Lanka">Sri Lanka</option>
            <option value="North Korea">North Korea</option>
            </select>
            <input type="date" class="span4" name="departure" placeholder="Departure date" /><br />
            <input type="text" class="span4" name="days" placeholder="Number of days" /><br />
            <input type="text" class="span4" name="travelers" placeholder="Number of travelers" /><br />
             <button class="btn btn-primary" type="submit" name="submit" value="Submit">Submit</button>
        </form>    

我的PHP代码如下:

<?php

if (isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['name'] ."\n"
.'Email: ' .$_POST['email'] ."\n"
.'Countries: ' .$_POST['country'] ."\n"
.'Departure Date: ' .$_POST['departure'] ."\n"
.'Number of Days: ' .$_POST['days'] ."\n"
.'Number of Travelers: ' .$_POST['travelers'] ."\n";



mail ('me@myemailaddress.com', 'Landing Page Enquiry', $msg);

 header ('location: send-success.php');

} else {
header('location: send-failed.php');
exit(0);
}
?>

1 个答案:

答案 0 :(得分:0)

您将表单元素命名为:

name="country[]" 

所以$ _POST ['country']是一个数组。如果你想要数组的所有元素,即所有选择,那么执行以下操作:

if (isset($_POST['submit'])) {
    $countries =  implode(' ', $_POST['country']);
}