【发布时间】:2015-11-02 21:19:52
【问题描述】:
我正在动态构建一个选择选项。我从选择选项中选择一个值。 (列表的值来自 php 如何将选定的值传递给 PHP?
<select id ="s1" name="swimopt" class="so">
<?php echo $options; ?>
</select>'
$options 来自 MySQL 并填充下拉列表
当我选择一个值,然后尝试
echo $_POST['swimopt'];
不显示所选值
请帮忙
<form id="swimdata" method="POST" action="save.php">
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="girls">GIRLS
<table id="meetTable" style="width:auto">
<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">
</form>
这是我的 PHP 从 mySQL 获取 $options
if ($result->num_rows > 0) {
$options= '';
// output data of each row
while($row = $result->fetch_assoc()) {
$options .= "<tr><td>" . $row["swimmer_id"]. "</td><td>" . $row["first_name"]. " " . $row["last_name"]. " </td><td> " . $row["school_name"]. "</td></tr>";
}
} else {
echo "0 results";
}
echo $options;
$conn->close();
截图:
选项回显到选择框中:
<select id ="s1" name="swimopt" class="so">
<?php echo $options; ?>
</select>'
PHP文件代码
$sql = "select first_name, last_name from swimming where gender='m'";
$results = mysqli_query($link,$sql);
if ($results->num_rows > 0) {
$options = '';
// output data of each row
while($row = $results->fetch_assoc()) {
$fname=$row["first_name"];
$lname=$row["last_name"];
$options .= "<option value= >" . $row["first_name"] . " " . $row["last_name"]."</option>";
}
} else {
echo "0 results";
}
echo $options;
$link->close();
?>
【问题讨论】:
-
显示你的完整代码 ;) 用表单和你的 php 部分周围的东西。
-
从我的 html 文件中添加了表单
-
我没有看到名为“swimopt”的输入。您是否尝试过
print_r($_POST)并查看通过了什么? -
'
-
选择驻留在表单中的什么位置?正如 Robbie 所说,当您提交表单时,在 PHP 端回显变量。使用
print_r($_POST)或var_dump($_POST)。