我根本无法弄清楚。我通过从数据库获取数据并填充下拉列表和表格来填充表格。我需要获取下拉值,一旦选择了该值,就需要通过获取数据的另一个mysql命令来更新表。我无法弄清楚如何获取下拉列表值来更改mysql查询和更新表。我的代码在下面。
<?php
session_start();
include '/Header.php';
include '/Footer.php';
include '/Functions.php';
extract($_POST);
$dbConnection = parse_ini_file("/db_connection.ini");
extract($dbConnection);
$myPdo = new PDO($dsn, $user, $password);
$myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function fill_drp($myPdo) {
$output = '';
$query = $myPdo->prepare("SELECT Term, Year FROM Semester");
$query->execute();
foreach ($query->fetchall(PDO::FETCH_ASSOC) as $row) {
$output .= '<option value="' . $row['Year'] . ' ' . $row['Term'] . '">' . $row['Year'] . ' ' . $row['Term'] . '</option>';
#$year = substr(($row['Year'] . $row['Term']), 2, 3); #need it to be 18W instead to query database
}
return $output;
}
function fill_table($myPdo, $selected_term) {
$output = '';
$query = $myPdo->prepare("select CourseOffer.CourseCode,
Title, WeeklyHours from CourseOffer join Course
on CourseOffer.CourseCode = Course.CourseCode
where SemesterCode= '" . $selected_term . "'");
$query->execute();
foreach ($query->fetchall(PDO::FETCH_ASSOC) as $row) {
$cc = $row['CourseCode'];
$output.= "<tr>";
$output.= "<td>" . $row['CourseCode'] . "</td>";
$output.= "<td>" . $row['Title'] . "</td>";
$output.= "<td> " . $row['WeeklyHours'] . "</td>";
$output.="<td> <input type='checkbox' name='chk[]' value='$cc'" . "</td>";
$output.= "</tr>";
}
return $output;
}
?>
<html>
<head>
<title>Online Course Registration</title>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<h1 class="text-center">Course Selection</h1>
<p>Welcome <strong><?php echo $_SESSION['name']; ?></strong> (not you? change user <a href="Login.php">here</a>)</p>
<p>You have registered <strong>#todo</strong> hours of course(s) for the semester</p>
<p>You can register <strong>#todo</strong> more hours of course(s) for the semester</p>
<p>Please note that the courses you have registered will not be displayed in the list</p>
<form action="CourseSelection.php" method="post">
<br/>
<div class="dropdown text-right">
<select class="dropdown" id="terms" name="terms" >
<?php
echo fill_drp($myPdo);
?>
</select>
</div>
<div id="tableContainer">
<table border="1" class="table" id="table1">
<thead class="thead-light">
<tr>
<th scope="col" >Code</th>
<th scope="col"> Course Title</th>
<th scope="col">Hours</th>
<th scope="col">Select</th>
</tr>
<?php
if ($_POST["term"] != null) {
$_SESSION['term'] = $_POST["term"];#attempt at getting selected dropdown value.
}
if (isset( $_SESSION['term'])) {
$selected_val = substr(($_SESSION['term']), 2, 3);
echo fill_table($myPdo, $selected_val);
} else {
echo fill_table($myPdo, '17F');#default is 17F but could be 18W 19S etc..
}
?>
</table>
</div>
<br/>
<input type='submit' class="btn btn-primary" class='button' name='submit' value='submit'/>
</form>
</body>
</html>
Ajax组件
我将此脚本添加到了表格的正下方
<script>
$(document).ready(function(){
$('#terms').change(function(){
var term = (($(this).val()).replace(' ','')).substring(2, 5);
//var term = '19W';
console.log(term); //shows that it's getting the semesters
$.ajax({
url:"RefreshTable.php",
method:"POST",
data:{term:term},
success:function(data){
$('#tableContainer').html(data);
}
});
});
});
</script>
我的refreshtable.php具有以下内容
<?php
$dbConnection = parse_ini_file("/db_connection.ini");
extract($dbConnection);
$myPdo = new PDO($dsn, $user, $password);
$myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$output = '';
if (isset($_POST["term"])) {
if ($_POST["term"] != '') {
$query = $myPdo->prepare("select CourseOffer.CourseCode,
Title, WeeklyHours from CourseOffer join Course
on CourseOffer.CourseCode = Course.CourseCode
where SemesterCode= '".$_POST["term"]."'");
} else {
$query = $myPdo->prepare("select CourseOffer.CourseCode,
Title, WeeklyHours from CourseOffer join Course
on CourseOffer.CourseCode = Course.CourseCode
where SemesterCode= '17F'");
}
$query->execute();
foreach ($query->fetchall(PDO::FETCH_ASSOC) as $row) {
$cc = $row['CourseCode'];
$output.= "<tr>";
$output.= "<td>" . $row['CourseCode'] . "</td>";
$output.= "<td>" . $row['Title'] . "</td>";
$output.= "<td> " . $row['WeeklyHours'] . "</td>";
$output.="<td> <input type='checkbox' name='chk[]' value='" . $row['CourseCode'] . "'" . "</td>";
#(isset($copies) ? $copies[$i] : '') . "' ></td>";
$output.= "</tr>";
}
echo $output;
}
?>
答案 0 :(得分:2)
您可以像在这里一样进行操作,对选择对象也可以使用相同的方法。
textbox onchange call php function
使用onchange调用函数进行Ajax调用,然后调用您的php脚本。
答案 1 :(得分:1)
您需要在fill_drp()
函数的下拉菜单中为选项分配一个值。
像这样:
$output .= '<option value="' . $row['Year'] . ' ' . $row['Term'] . '">' . $row['Year'] . ' ' . $row['Term'] . '</option>';