我有一个由MYSQL表填充的下拉菜单。我已经包含了学院的ID和名称。我想将下拉菜单中所选项目的ID发布到下一页。这是addstudent.php。 PHP代码如下。
<form action="addstudent.php" method="post">
<div class="input-group">
<?php
echo '<select class="form-control" id="sel1">';
while ($row = mysqli_fetch_array($result)) {
echo '<option value = "'.$row['faculty_id'].'">' . $row['name'] . '</option>';
}
echo '</select>';
?>
<span class="input-group-btn ml-2">
<button class="btn btn-primary" type="submit">Add Student</button>
</span>
</div>
</form>
未定义索引上的代码位于addstudent.php页面上。代码如下
if (isset($_POST['faculty_id'])) {
$faculty_id = $_POST['faculty_id'];
} else{
echo "Something is wrong";
}
答案 0 :(得分:3)
您错过了select
元素中的name属性。
将name
添加到select
元素,如下所示:
echo '<select class="form-control" id="sel1" name="faculty_id">';
您将在第二页上使用表单发布方法获取发布请求。因此,请使用$_POST
超全局的页面上的name属性以及字段的给定名称:$_POST['faculty_id']
答案 1 :(得分:1)
试试这个:
<form action="addstudent.php" method="post">
<div class="input-group">
<?php
echo '<select class="form-control" id="sel1" name="faculty_id">';
while ($row = mysqli_fetch_array($result)) {
echo '<option value = "'.$row['faculty_id'].'">' . $row['name'] . '</option>';
}
echo '</select>';
?>
<span class="input-group-btn ml-2">
<button class="btn btn-primary" name="addstudent" type="submit">Add Student</button>
</span>
</div>
</form>
你的php代码有一些错误检查这个
if (isset($_POST['faculty_id'])) { // OR if(isset($_POST['addstudent']))
$faculty_id = $_POST['faculty_id']; // Here the name of select not the value
} else{
echo "Something is wrong";
}