我在一张桌子的招待所中有5个房间,用户必须检查他们想要预订的一个或多个房间,并选择他们希望拥有的选项,例如人数,早餐选项,晚餐选项和床选项。
假设我们按出现顺序将这5个房间称为:
元素室
地球室
水室
飞机室
消防室
如果我按从上到下的顺序选择房间(例如元素到火的房间),则会得到结果选项,但是如果我选择两者之间的任何房间,则得到0。当从复选框中选中房间名称时,它的名称总是正确显示,但是选项值显示为0(第一个房间(元素)除外)
HTML:
<form class="booking-form" name="book-room-form" action="" id="contactForm" method="post" novalidate>
<?php foreach ( $rooms as $room ) : ?>
<input type="checkbox" class="form-check-input" name="room-selected[]" value="<?php echo $room->post_title; ?>">
<select class="num-person select-update" id="<?php echo 'r-' . $room->ID; ?>" name="people-select[]" required>
<option value="0" selected>Select Number of Persons</option>
<option value="1">1 person R600</option>
<option value="2">2 persons R800</option>
</select>
<select class="num-dinner select-update" id="<?php echo 'd-' . $room->ID; ?>" name="dinner-select[]">
<option value="0" selected>Select Dinner Course</option>
<option value="120">Two Course Dinner R120</option>
<option value="200">Three Course Dinner R200</option>
</select>
<select class="num-bf select-update" id="<?php echo 'b-' . $room->ID; ?>" name="breakfast-select[]">
<option value="0" selected>Select Breakfast Type</option>
<option value="70">Basic Breakfast R70</option>
<option value="120">Full Breakfast R120</option>
</select>
<select class="bed-select" name="bed-select[]">
<option selected>Select Bed Size</option>
<option value="King Bed">King Bed</option>
<option value="2 Single Beds">2 Single Beds</option>
</select>
<?php endforeach; ?>
<button name="submit-request" type="submit" class="btn btn-primary">Submit</button>
</form>
代码:
if(isset($_POST['submit-request'])) {
$room_selected = $_POST['room-selected'];
$numPeople = $_POST['people-select'];
$dinnerSelect = $_POST['dinner-select'];
$breakfastSelect = $_POST['breakfast-select'];
$bedSelect = $_POST['bed-select'];
$room = $room_selected;
$num = $numPeople;
$dinn = $dinnerSelect;
$bf = $breakfastSelect;
$bed = $bedSelect;
foreach ($room as $id => $key) {
//$key returns the room name
if($key) {
$result[$key] = array(
'num_person' => $num[$id],
'dinner' => $dinn[$id],
'breakfast' => $bf[$id],
'bed_type' => $bed[$id],
);
}
echo $key . '<br/>' . $num[$id] . '<br/>' . $dinn[$id] . '<br/>' . $bf[$id] . '<br/>' . $bed[$id] . '<br/>' ;
}
}
我希望能够选择任何房间并显示选项值
答案 0 :(得分:1)
我之所以这样说是因为您没有在html表单数组中使用任何键。当前的解决方案有些脆弱,因此您需要在字段命名中更加具体,或者将键包含在数组中。
<?php foreach ( $rooms as $room ) : ?>
<input type="checkbox" class="form-check-input" name="room-selected[]" value="<?php echo $room->post_title; ?>">
<select class="num-person select-update" id="<?php echo 'r-' . $room->ID; ?>" name="people-select[<?php echo $room->post_title; ?>]" required>
<option value="0" selected>Select Number of Persons</option>
<option value="1">1 person R600</option>
<option value="2">2 persons R800</option>
</select>
<select class="num-dinner select-update" id="<?php echo 'd-' . $room->ID; ?>" name="dinner-select[<?php echo $room->post_title; ?>]">
<option value="0" selected>Select Dinner Course</option>
<option value="120">Two Course Dinner R120</option>
<option value="200">Three Course Dinner R200</option>
</select>
<select class="num-bf select-update" id="<?php echo 'b-' . $room->ID; ?>" name="breakfast-select[<?php echo $room->post_title; ?>]">
<option value="0" selected>Select Breakfast Type</option>
<option value="70">Basic Breakfast R70</option>
<option value="120">Full Breakfast R120</option>
</select>
<select class="bed-select" name="bed-select[<?php echo $room->post_title; ?>]">
<option selected>Select Bed Size</option>
<option value="King Bed">King Bed</option>
<option value="2 Single Beds">2 Single Beds</option>
</select>
<?php endforeach; ?>
然后,在运行循环而不是检查$ id时,请使用$ key。
if(isset($_POST['submit-request'])) {
$room_selected = $_POST['room-selected'];
$numPeople = $_POST['people-select'];
$dinnerSelect = $_POST['dinner-select'];
$breakfastSelect = $_POST['breakfast-select'];
$bedSelect = $_POST['bed-select'];
$room = $room_selected;
$num = $numPeople;
$dinn = $dinnerSelect;
$bf = $breakfastSelect;
$bed = $bedSelect;
foreach ($room as $id => $key) {
//$key returns the room name
if($key) {
$result[$key] = array(
'num_person' => $num[$key],
'dinner' => $dinn[$key],
'breakfast' => $bf[$key],
'bed_type' => $bed[$key],
);
}
echo $key . '<br/>' . $num[$key] . '<br/>' . $dinn[$key] . '<br/>' . $bf[$key] . '<br/>' . $bed[$key] . '<br/>' ;
}
}