我已经创建了一个预订表格。用户在从下拉菜单中选择住宿选项时可以选择不选择住宿选项,因此值为“选择一个...”。如果他们没有选择住宅区,一旦他们提交了表格,就会将他们带到另一个页面,根据他们的班级和偏好给他们提供他们为住宿区域选择的下拉列表。表格的结果打印在第三页上,其名称,性别,居住区域等等。问题是我使用$ _POST [“宿舍”]检索居住区域。这在用户确实选择了居住区域时有效,但如果没有,则$ _POST似乎不会更新并显示为“选择一个......”。
以下是我为该用户提供居住区选项的页面代码:
<html>
<?php
require 'sql_helper.php';
echo " <form action = \"results2.php\" method = \"post\">\n<br> ";
//Read the information entered on the index page
$specialNeeds = isset($_POST["specialNeeds"]);
$laundry = isset($_POST["laundry"]);
$fullyEquippedKitchen = isset($_POST["fullyEquippedKitchen"]);
$class = $_POST["class"];
$dorm = $_POST["dorm"];
//If user did not choose a dorm
if ($dorm == "Select One...") {
$sql = "SELECT name, roomsAvailable FROM $dormTable WHERE class = $class || class = 'Select One...'";
if($result = mysqli_query($conn, $sql)) {
$numRows = mysqli_num_rows($result);
echo " <strong>Residence Areas</strong>\n ";
echo " <select name = dorm> \n";
for ($i = 0; $i < $numRows; $i++){
$aDorm = mysqli_fetch_assoc($result);
$dormName = $aDorm['name'];
$dormAvailable = $aDorm['roomsAvailable'];
if($dormName != 'Select One...'){
echo "<option value = \"$dormName\" > $dormName ($dormAvailable)</option>\n";
}
elseif ($dormAvailable == 0 && $dormName != 'Select One...'){
echo "<option value = \"$dormName\" disabled=\"disabled\"> $dormName </option> \n";
}
elseif ($dormName == 'Select One...'){
echo "<option value = \"$dormName\" disabled=\"disabled\" selected = \"true\"> $dormName </option> \n";
}
}
echo "</select>\n<br><br>";
}
foreach ($_POST as $k => $v){
echo"<input type = hidden name = $k value = \"$v\" ><?php echo print_r($_POST) ?>";
}
echo " <input type = \"submit\" value = \"Submit\"> \n <br> ";
}
//If user did choose a dorm
else {
$sql = "SELECT * FROM $dormTable WHERE name = '$dorm'";
if ($result = mysqli_query($conn, $sql)) {
$dormRecord = mysqli_fetch_assoc($result);
if ($class == $dormRecord['class']) {
unset($errorString);
// check preferences
if ($fullyEquippedKitchen) {
if (!$dormRecord['fullyEquippedKitchen']) {
$errorString = "$dormRecord[name] does not have a kitchen.\n";
echo $errorString;
echo 'Go back to the previous page to re-enter your preferences.<br><br>';
echo '<a href = "index2.php">Go back</a><br><br>';
}
else{
foreach ($_POST as $k => $v){
echo"<input type = hidden name = $k value = \"$v\"> <?php echo print_r($_POST) ?>";
}
echo "Your preferences are a match!<br>";
echo " <input type = \"submit\" value = \"Submit\"> \n <br> ";
}
}
else{
foreach ($_POST as $k => $v){
echo"<input type = hidden name = $k value = \"$v\"> <?php echo print_r($_POST) ?>";
}
echo "Your preferences are a match!<br>";
echo " <input type = \"submit\" value = \"Submit\"> \n <br> ";
}
}
else {
$errorString = "You cannot live in $dormRecord[name] because of your class status.\n";
echo $errorString;
echo 'Go back to the previous page to re-enter your preferences.<br><br>';
echo '<a href = "index2.php">Go back</a><br><br>';
}
}
else {
echo "something is wrong: ".mysqli_error($conn);
die;
}
?>
</form>
<?php
}
?>