PHP下拉框提交冲突:如何同时展示选中的飞机与座椅重量数据
问题
开发了一个包含两个下拉框的网页,分别用于选择飞机型号(Aircraft Type)和飞行员座椅类型(Pilot Seat Type)。目前遇到的问题是,选择其中一个下拉框的选项后,另一个下拉框对应的展示内容会消失。希望能同时展示选中飞机型号的基本重量(basicWeight),以及选中飞行员座椅类型的座椅重量(pilotSeatWeight)。
预期效果:
- 选择飞机型号M503-1时,页面显示该机型的基本重量;
- 再选择飞行员座椅类型Captain时,页面同时展示M503-1的基本重量和Captain座椅的重量。
尝试使用session变量存储$basicWeight和$pilotSeatWeight的值,但未达到预期效果,询问如何基于两个下拉框的选择,同时输出这两个重量值。
问题分析
你的代码核心问题在于:
- 两个独立表单提交:每次提交只有一个下拉框的参数被传递到后端,另一个参数缺失,导致后端无法保留之前的选中状态,对应数据被重置为初始值0。
- Session使用错误:开头
session_start()后直接将$_SESSION['selectedAircraftID']设为0,直接覆盖了之前存储的会话值,等于没利用session保存状态。 - 冗余数据库查询:重复引入
db.php并多次查询同一份飞机数据,既低效又容易引发变量覆盖问题。 - SQL注入风险:直接将POST参数拼接到SQL语句中,存在严重的安全隐患。
解决方案
核心修改思路
- 把两个下拉框整合到同一个表单中,确保每次提交都能传递所有选中状态;
- 正确使用Session:先从Session读取历史选中值,再用本次POST提交的值覆盖,最后将最新状态存回Session;
- 合并重复的数据库查询,优化代码结构;
- 使用PHP预处理语句避免SQL注入;
- 简化前端提交逻辑,去掉独立的提交函数。
修改后的完整代码
<?php session_start(); // 初始化默认值,优先从Session读取历史选中状态 $selectedAircraftID = $_SESSION['selectedAircraftID'] ?? '-'; $selectedPilotSeatType = $_SESSION['selectedPilotSeatType'] ?? '-'; // 处理表单提交,更新选中状态 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $selectedAircraftID = $_POST['aircraftType'] ?? '-'; $selectedPilotSeatType = $_POST['pilotSeatType'] ?? '-'; // 将最新选中状态存入Session $_SESSION['selectedAircraftID'] = $selectedAircraftID; $_SESSION['selectedPilotSeatType'] = $selectedPilotSeatType; } // 引入数据库连接 include 'db.php'; // 初始化飞机数据 $basicWeight = '0'; $CGArm = '0'; $lat = '0'; // 查询飞机数据(选中有效型号时) if ($selectedAircraftID !== '-') { $stmt = $conn->prepare("SELECT basicWeight, CGArm, lat FROM aircraft WHERE aircraftID = ?"); $stmt->bind_param("s", $selectedAircraftID); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $basicWeight = $row['basicWeight']; $CGArm = $row['CGArm']; $lat = $row['lat']; } $stmt->close(); } // 初始化座椅数据 $pilotSeatWeight = '0'; $pilotSeatLong = '0'; $pilotSeatLat = '0'; // 查询座椅数据(选中有效类型时) if ($selectedPilotSeatType !== '-') { $stmt = $conn->prepare("SELECT seatWeight, seatLong, seatLat FROM mainseat WHERE seatID = ?"); $stmt->bind_param("s", $selectedPilotSeatType); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $pilotSeatWeight = $row['seatWeight']; $pilotSeatLong = $row['seatLong']; $pilotSeatLat = $row['seatLat']; } $stmt->close(); } $conn->close(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Weight and balance calculation</title> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet"> <link rel="stylesheet" href="styles/home.css"> </head> <body> <div class="aircraft-details-container"> <div class="aircraft-details-header">Aircraft Details</div> <table class="aircraft-details-table"> <thead> <tr> <th class="aircraft-details-th">DATE</th> <th class="aircraft-details-th">AIRCRAFT</th> <th class="aircraft-details-th">BASIC WEIGHT</th> <th class="aircraft-details-th">C OF G</th> <th class="aircraft-details-th">LAT(MM)</th> </tr> </thead> <tbody> <tr> <td class="aircraft-details-td"> <input class="aircraft-details-date" type="date" name="selectedDate"> </td> <td class="aircraft-details-td"> <!-- 单个表单包裹所有需要提交的控件 --> <form method="POST" id="mainForm"> <select class="aircraft-select" name="aircraftType" onchange="document.getElementById('mainForm').submit()"> <option value="-" <?php echo $selectedAircraftID === '-' ? 'selected' : ''; ?>>N/A</option> <?php $stmt = $conn->prepare("SELECT aircraftID FROM aircraft"); $stmt->execute(); $result = $stmt->get_result(); while($row = $result->fetch_assoc()) { $aircraftID = $row['aircraftID']; $selected = ($aircraftID === $selectedAircraftID) ? 'selected' : ''; echo "<option value='$aircraftID' $selected>$aircraftID</option>"; } $stmt->close(); ?> </select> </td> <td class='aircraft-details-td'><?php echo $basicWeight ?></td> <td class='aircraft-details-td'><?php echo $CGArm ?> </td> <td class='aircraft-details-td'><?php echo $lat ?> </td> </tr> </tbody> </table> </div> <!---Passenger Details Table ---> <div class="passenger-details-container"> <div class="passenger-details-header">Passenger Details</div> <table class="passenger-details-table"> <thead> <tr> <th class="passenger-details-th">ITEM</th> <th class="passenger-details-th">WIGHT (KG)</th> <th class="passenger-details-th">SEAT</th> <th class="passenger-details-th">ROW</th> <th class="passenger-details-th">SEAT WEIGHT (KG)</th> <th class="passenger-details-th">CG ARM (MM)</th> <th class="passenger-details-th">MOMENTS LONG (KGMM)</th> <th class="passenger-details-th">LAT(MM)</th> <th class="passenger-details-th">MOMENTS LAT (KGMM)</th> </tr> </thead> <tbody> <!--BASIC WEIGHT ROW ---> <tr> <td class='passenger-details-td'>Basic Weight</td> <td class='passenger-details-td'><?php echo $basicWeight; ?></td> <td class='passenger-details-td'>N/A</td> <td class='passenger-details-td'>N/A</td> <td class='passenger-details-td'>N/A</td> <td class='passenger-details-td'><?php echo $CGArm; ?></td> <td class='passenger-details-td'><?php echo(floatval($basicWeight) * floatval($CGArm)); ?></td> <td class='passenger-details-td'><?php echo $lat; ?></td> <td class='passenger-details-td'><?php echo (floatval($basicWeight) * floatval($lat)); ?></td> </tr> <!--PILOT ROW ---> <tr> <td class='passenger-details-td'>Pilot</td> <td class='passenger-details-td'>80.0</td> <td class='passenger-details-td'> <select class="select-main" name="pilotSeatType" onchange="document.getElementById('mainForm').submit()"> <option value="-" <?php echo $selectedPilotSeatType === '-' ? 'selected' : ''; ?>>N/A</option> <option value="pilotSeat" <?php echo $selectedPilotSeatType === 'pilotSeat' ? 'selected' : ''; ?>>Pilot Seat</option> <option value="coPilotSeat" <?php echo $selectedPilotSeatType === 'coPilotSeat' ? 'selected' : ''; ?>>Co Pilot Seat</option> </select> </td> <td class='passenger-details-td'>N/A</td> <td class='passenger-details-td'><?php echo $pilotSeatWeight; ?></td> <td class='passenger-details-td'><?php echo $pilotSeatLong; ?></td> <td class='passenger-details-td'><?php echo (floatval(80) * floatval($pilotSeatWeight)); ?></td> <td class='passenger-details-td'><?php echo $pilotSeatLat; ?></td> <td class='passenger-details-td'><?php echo (floatval(80) * floatval($pilotSeatLat)); ?></td> </tr> </tbody> </table> </form> <!-- 闭合主表单 --> </div> </body> </html>
关键修改点说明
- 统一表单:将两个下拉框放入同一个表单
mainForm,每次提交都会传递两个下拉框的选中值,确保后端能同时获取到两组状态。 - Session正确使用:先从Session读取历史选中值,再用本次POST的值更新,最后存回Session,实现状态持久化。
- 预处理语句:所有数据库查询都使用
prepare和bind_param,彻底避免SQL注入风险。 - 代码精简:合并重复的数据库查询,去掉冗余的变量初始化和重复引入
db.php,让代码更简洁高效。 - 前端逻辑简化:去掉独立的提交函数,直接在下拉框的
onchange事件中提交主表单。
内容的提问来源于stack exchange,提问作者Najmuddin Roshenry
相关产品推荐
相关产品推荐

