0

无法让我的表单从多选选项(在下面的编码中命名为“国家”)发送数据。我已经在这里查看了一些答案,但我真的不是 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);
}
?>
4

1 回答 1

0

您将表单元素命名为:

name="country[]" 

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

if (isset($_POST['submit'])) {
    $countries =  implode(' ', $_POST['country']);
}
于 2013-04-22T03:03:43.013 回答