通过PHP mysql PDO基于下拉列表选择的值更新表



我根本想不出来。我通过从数据库中获取数据并填充下拉列表和表来填充表。我需要获得下拉值,一旦选择了该值,我就需要通过另一个获取数据的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;
}
?>

您可以像在这里一样进行操作,与select的操作方式相同
textbox onchange调用php函数
用onchange调用函数执行Ajax调用并调用php脚本。

您需要在fill_drp()函数的下拉列表中为选项分配一个值。

像这样:

$output .= '<option value="' . $row['Year'] . ' ' . $row['Term'] . '">' . $row['Year'] . ' ' . $row['Term'] . '</option>';

最新更新