我在html中有一个下拉列表<select>
列表。用户提交表单后,如何识别$_POST数组中的选定值?
<form action="subj_exec.php">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<br>
<select name = "subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value=';
echo $subject_id;
echo '> ';
echo $name;
echo '</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
</form>
subject_id在另一个php文件中为空
echo $_POST['subject_id']
为空。
请帮助识别代码中的问题。
谢谢,
表单的标准方法是GET
,因此需要将method="POST"
添加到表单中。
-
get:默认值。以名称/值对将表单数据追加到URL:URL?name=值&name=值
-
post:将表单数据作为HTTP后事务发送
<form action="subj_exec.php" method="POST"> //<<<< added method <?php echo $_SESSION['SESS_MEMBER_ID']; echo $_SESSION['SESS_FIRST_NAME']; echo $_SESSION['SESS_LAST_NAME']; ?> <br> <select name = "subj_id"> <?php while ($row = mysqli_fetch_array($result)) { $subject_id = $row['id']; $code = $row['code']; $name = $row['name']; ?> <option value="<?= $subject_id; ?>"><?= $name; ?></option> <?php } ?> </select> <input type="submit" value="submit" name="submit" />
在您的文件subj_exec.php中,您可以使用输出选定的值
echo $_POST['subj_id'];
开始:
index.php
<form action="subj_exec.php" method="POST">
<?php
echo $_SESSION['SESS_MEMBER_ID'];
echo $_SESSION['SESS_FIRST_NAME'];
echo $_SESSION['SESS_LAST_NAME'];
?>
<select name="subj_id">
<?php
while ($row = mysqli_fetch_array($result)) {
$subject_id = $row['id'];
$code = $row['code'];
$name = $row['name'];
echo '<option value="'.$subject_id.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" value="submit" name="submit" />
subj_exec.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit'])) {
if(strlen($_POST['subj_id']) >= 1) {
$option = htmlentities($_POST['subj_id'], ENT_QUOTES, "UTF-8");
// Do Something here with $option
echo $option;
}else {
echo 'nothing selected.';
}
}
?>