我有一个包含多个复选框的表单。在提交时,我希望能够获取复选框并能够将它们存储在sql中。我正在使用的foreach不工作,我尝试了很多其他方法,但没有一个工作。请帮忙谢谢!
我从帖子开头就更新了我的代码以解决问题,但仍然遇到问题。
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
session_start();
if(isset($_POST['Submitme'])){
if(!empty($_POST['flight'])){
print_r($_POST);
foreach($_POST['flight'] as $key=>$selected){
echo $selected."</br>";
}
}
}
?>
<div class="plane">
<div class="cockpit" >
<h1>Please select a seat</h1>
</div>
<div class="exit exit--front fuselage text-center">
</div>
<form action="flightselect.php" method="post" name="form">
<ol class="cabin fuselage">
<li class="row row--1" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="1A" value="1A" />
<label for="1A">1A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="1B" value="1B" />
<label for="1B">1B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="1C" value="1C" />
<label for="1C">1C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="1D" value="1D" />
<label for="1D">1D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="1E" value="1E" />
<label for="1E">1E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="1F" value="1F" />
<label for="1F">1F</label>
</li>
</ol>
</li>
<li class="row row--2" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="2A" value="2A" />
<label for="2A">2A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="2B" value="2B" />
<label for="2B">2B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="2C" value="2C" />
<label for="2C">2C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="2D" value="2D" />
<label for="2D">2D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="2E" value="2E" />
<label for="2E">2E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="2F" value="2F" />
<label for="2F">2F</label>
</li>
</ol>
</li>
<li class="row row--3" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="3A" value="3A" />
<label for="3A">3A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="3B" value="3B" />
<label for="3B">3B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="3C" value="3C" />
<label for="3C">3C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="3D" value="3D" />
<label for="3D">3D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="3E" value="3E" />
<label for="3E">3E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="3F" value="3F" />
<label for="3F">3F</label>
</li>
</ol>
</li>
<li class="row row--4" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="4A" value="4A" />
<label for="4A">4A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="4B" value="4B" />
<label for="4B">4B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="4C" value="4C" />
<label for="4C">4C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="4D" value="4D" />
<label for="4D">4D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="4E" value="4E" />
<label for="4E">4E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="4F" value="4F" />
<label for="4F">4F</label>
</li>
</ol>
</li>
<li class="row row--5" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="5A" value="5A" />
<label for="5A">5A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="5B" value="5B" />
<label for="5B">5B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="5C" value="5C" />
<label for="5C">5C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="5D" value="5D" />
<label for="5D">5D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="5E" value="5E" />
<label for="5E">5E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="5F" value="5F" />
<label for="5F">5F</label>
</li>
</ol>
</li>
<li class="row row--6" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="6A" value="6A" />
<label for="6A">6A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="6B" value="6B" />
<label for="6B">6B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="6C" value="6C" />
<label for="6C">6C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="6D" value="6D" />
<label for="6D">6D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="6E" value="6E" />
<label for="6E">6E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="6F" value="6F" />
<label for="6F">6F</label>
</li>
</ol>
</li>
<li class="row row--7" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="7A" value="7A" />
<label for="7A">7A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="7B" value="7B" />
<label for="7B">7B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="7C" value="7C" />
<label for="7C">7C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="7D" value="7D" />
<label for="7D">7D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="7E" value="7E" />
<label for="7E">7E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="7F" value="7F" />
<label for="7F">7F</label>
</li>
</ol>
</li>
<li class="row row--8" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="8A" value="8A" />
<label for="8A">8A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="8B" value="8B" />
<label for="8B">8B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="8C" value="8C" />
<label for="8C">8C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="8D" value="8D" />
<label for="8D">8D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="8E" value="8E" />
<label for="8E">8E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="8F" value="8F" />
<label for="8F">8F</label>
</li>
</ol>
</li>
<li class="row row--9" style="padding-left: 15%;">
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="9A" value="9A" />
<label for="9A">9A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="9B" value="9B" />
<label for="9B">9B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="9C" value="9C" />
<label for="9C">9C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="9D" value="9D" />
<label for="9D">9D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="9E" value="9E" />
<label for="9E">9E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="9F" value="9F" />
<label for="9F">9F</label>
</li>
</ol>
</li>
<li class="row row--10" style="padding-left: 4%";>
<ol class="seats" type="A">
<li class="seat">
<input type="checkbox" name="flight[]" id="10A" value="10A" />
<label for="10A">10A</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="10B" value="10B" />
<label for="10B">10B</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="10C" value="10C" />
<label for="10C">10C</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="10D" value="10D" />
<label for="10D">10D</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="10E" value="10E" />
<label for="10E">10E</label>
</li>
<li class="seat">
<input type="checkbox" name="flight[]" id="10F" value="10F" />
<label for="10F">10F</label>
</li>
</ol>
</li>
</ol>
<div class="exit exit--back fuselage">
</div>
</div>
<input type="submit" name="seatme" value="Submit" style="font-weight: bold;">
</form>
答案 0 :(得分:0)
您忘了为您提供价值checkboxes
只是为所有复选框提供价值,然后重试。
<input type="checkbox" name="flight[]" id="2A" value='2A' />
将PHP部分更改为以下
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
session_start();
if(isset($_POST['SubmitFlight'])){
if(!empty($_POST['flight'])){
foreach($_POST['flight'] as $key=>$selected){
echo $selected."</br>";
}
}
}
?>