php - PHP在新表单后删除变量
问题描述
在我的代码中,我有两种形式供用户选择选项。第一个变量将保存,但一旦用户提交第二个表单,第一个表单中的变量就不再保存。
<div class = "school">
<h3>Please select the university you previously attended</h3>
<form action = "" method = "post" name = "school_form">
<select name="school" size ="10">
<?php
//shows options for $selected_school
$sql = "SELECT DISTINCT school FROM data;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0){
while($row = mysqli_fetch_assoc($result)){
// inserts all data as array
echo "<option>". $row['school'] ."</option>";
}
}
?>
</select>
<br>
<input type ="submit" name = "submit_school" value = "Enter">
</form>
<?php
//saves selected option as $selected_school
if(isset($_POST['submit_school'])){
$selected_school = mysqli_real_escape_string($conn, $_POST['school']);
echo "You have selected: " .$selected_school;
}
?>
</div>
<div class ="courses">
<h3>Please select the courses you took</h3>
<form action = "" method ="post" name ="course_form">
<?php
//user shown options for courses
$sql2 = "SELECT transfer_course, transfer_title FROM data WHERE school = ? ORDER BY transfer_course ASC";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql2)) {
echo "SQL statement failed";
} else {
mysqli_stmt_bind_param($stmt, "s", $selected_school);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
while($row2 = mysqli_fetch_assoc($result2)){
echo "<input type='checkbox' name ='boxes[]' value = '" . $row2['transfer_course'] . "' >" . $row2['transfer_course'] . "<br>";
}
}
?>
<br>
<input type ="submit" name = "submit_courses" value = "Enter">
</form>
<br>
<?php
//saved selected option(s) as $selected_course
if(isset($_POST['submit_courses'])){//to run PHP script on submit
if(!empty($_POST['boxes'])){
foreach($_POST['boxes'] as $selected_course){
echo "You have selected: " . $selected_course . "</br>";
}
}
}
?>
</div>
<div class = "output">
<h3>Course Equivalency</h3>
<?php
$sql3 = "SELECT arcadia_course, arcadia_title FROM data WHERE school = " . $selected_school . " AND transfer_course = " . $selected_course . "";
$result3 = mysqli_query($conn, $sql3);
if($result3)
{
while($row3 = mysqli_fetch_assoc($result3)){
echo $row3['arcadia_course'] . " " . $row3['arcadia_title'] . "<br>";
}
} else {
echo "failed";
echo $sql3;
}
?>
所以当我到达我的下一个 sql 语句时
$sql3 = "SELECT arcadia_course, arcadia_title FROM data WHERE school = " . $selected_school . " AND transfer_course = " . $selected_course . "";
When the school is selected, it saves the variable, but when the course is selected, $selected_school becomes blank again.
我已经在页面顶部有 session_start() 。
解决方案
看起来您的选项没有传递的值。在你的第一种形式中尝试这个 while 循环:
echo '<option value="' . $row['school'] . '">' . $row['school'] . '</option>';
看起来您可能还有更多问题。如果这不能解决您的问题,我会深入挖掘。
编辑:然后,是的,正如其他人所建议的那样,您可能还想添加一个隐藏的输入字段以在第二个表单提交上传递该变量值。
我们所说的隐藏输入字段是这样的:
<input type="hidden" name="selected_school" value="<?php if(isset($selected_school) echo $selected_school; ?>">
推荐阅读
- debugging - simple-pt 中的 spdecode 响应“错误跟踪流与查询不匹配”英特尔处理器跟踪
- html - 结合弹性和绝对定位时的困惑
- function - 带有循环的函数以包含重要代码
- dictionary - 进行基于字典的文本分类时如何创建字典?价值观是如何确定的?
- python - 如何使用外部变量中断和停止正在运行的进程
- excel - 在 Excel 中满足条件的 VLOOKUP 函数的总和
- python-3.x - 以数据框为输入并行化函数
- angular - 在 .NET 应用程序中包含 Angular
- javascript - 类型“ArrayBuffer”不可分配给类型“字符串”
- r - 根据年份输入创建加权平均值和 3 年平均值