使用PHP接收选择多个值

时间:2018-05-07 12:34:50

标签: php bootstrap-4

我试图在bootstrap中使用select multiple接收多个选定的值。我也尝试使用数组,因为我在互联网上找到了几个与接收选择框值相关的解决方案,但仍然无法正常工作。它还发送只写的Array字,没有值。

Html代码

<form action="send-mail.php" method="POST" class="contact_box">
                            <div class="panel-body">
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user blue"></i></span>
                                        <input type="text" name="InputName" placeholder="Full Name*" class="form-control" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-envelope blue"></i></span>
                                        <input type="email" name="InputEmail" placeholder="Email Address*" class="form-control" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-phone blue"></i></span>
                                        <input type="number" name="InputCno" placeholder="Phone Number*" class="form-control" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-picture blue"></i></span>
                                        <input type="text" class="form-control" placeholder="Services Required* (Hold Ctrl to Select More than One Service)" disabled>
                                           <select multiple class="form-control" name="Services[]" placeholder="Services Required*" required size="3">
                                             <option value="s1">1</option>
                                             <option value="s2">2</option>
                                             <option value="s3">3</option>
                                             <option value="s4">4</option>
                                             <option value="s5">5</option>
                                             <option value="s6">6</option>
                                           </select>

                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-comment blue"></i></span>
                                        <textarea name="InputMessage" rows="6" placeholder="Message or Comment*" class="form-control" type="text" required></textarea>
                                    </div>
                                </div>
                                <div class="">
                                <button type="submit" class="btn btn-default pull-right">Send <span class="glyphicon glyphicon-send"></span></button>
                                    <button type="reset" value="Reset" name="reset" class="btn">Reset <span class="glyphicon glyphicon-refresh"></span></button>
                                </div>
                            </div>
                    </form>

PHP代码

<?php
$InputName = $_POST['InputName'];
$InputEmail = $_POST['InputEmail'];
$InputCno = $_POST['InputCno'];
$Services = $_POST['Services'];
$InputMessage = $_POST['InputMessage'];
$formcontent=" From: $InputName \n Email Address: $InputEmail \n Contact Number: $InputCno \n Services Required: $Services \n Message: $InputMessage ";
$recipient = "test@example.com";
$subject = "Enquiry";
$mailheader = "From: $InputEmail \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: thank-you.html');
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>

请帮助您在选择框中接收所有选定的值。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

你的问题是你想要访问数组内容,这不是由echo,

完成的

您可以使用implode函数将数组内容转换为字符串

$Services = implode(",",$_POST['Services']);