<!DOCTYPE html>
<html lang="en">
<head>
<?php
$objConnect = mysql_connect("localhost","root","ad001") or die("Error Connect to Database");
$objDB = mysql_select_db("db_timetable");
@mysql_query("SET NAMES UTF8");
?>
<script language="JavaScript">
function Listname(SelectValue)
{
frmregis.subname.length = 0
frmregis.subsec.length = 0
var myOption = new Option('Select Subject Name','0')
frmregis.subname.options[frmregis.subname.length]= myOption
<?php
$intRows = 0;
$strSQL = "SELECT DISTINCT sub_id FROM subject ORDER BY sub_id ASC";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
?>
x = <?php echo $intRows;?>;
mySubList = new Array();
strGroup = <?php echo $objResult["sub_id"];?>;
strValue = "<?php echo $objResult["sub_name"];?>";
strItem = "<?php echo $objResult["sub_name"];?>";
mySubList[x,0] = strItem;
mySubList[x,1] = strGroup;
mySubList[x,2] = strValue;
if (mySubList[x,1] == SelectValue){
var myOption = new Option(mySubList[x,0], mySubList[x,2])
frmregis.subname.options[frmregis.subname.length]= myOption
}
<?php
}
?>
}
function Listsec(SelectValue)
{
frmregis.subsec.length = 0
var myOption = new Option('Select Section','0')
frmregis.subsec.options[frmregis.subsec.length]= myOption
<?php
$intRows = 0;
$strSQL = "SELECT sub_name FROM subject ORDER BY sub_id ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
?>
x = <?php echo $intRows;?>;
mySubList = new Array();
strGroup = <?php echo $objResult["sub_name"];?>;
strValue = "<?php echo $objResult["sub_sec"];?>";
strItem = "<?php echo $objResult["sub_sec"];?>";
mySubList[x,0] = strItem;
mySubList[x,1] = strGroup;
mySubList[x,2] = strValue;
if (mySubList[x,1] == SelectValue){
var myOption = new Option(mySubList[x,0], mySubList[x,2])
frmregis.subsec.options[frmregis.subsec.length]= myOption
}
<?php
}
?>
}
</script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<!-- CSS -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/form-elements.css">
<link rel="stylesheet" href="assets/css/style.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="row">
<div class="col-sm-6 col-sm-offset-3 form-box">
<div class="form-top">
<div class="form-top-left">
<h3>Register</h3>
<p>Please Select Information</p>
</div>
<div class="form-top-right">
<i class="fa fa-table"></i>
</div>
</div>
<div class="form-bottom">
<form role="form" action="checkregister.php" method="post" class="register" name="frmregis">
<div class="form-group">
<div class="row">
<div class="col-md-4">
<label for="studentid" class=".col-xs-4 "> Student ID : </label>
</div>
<div class=" col-xs-4 col-sm-4 col-lg-4">
<select class="form-control input-lg" name="stdid">
<option value="">-- Select Student ID --</option>
<?php
$strSQL = "SELECT * FROM student ORDER BY std_id ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?php echo $objResult["std_id"];?>"><?php echo $objResult["std_id"];?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<label for="subjectid" class=".col-xs-4 "> Subject ID : </label>
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<select class="form-control input-lg" name="subid" OnChange="Listname(this.value);">
<option value="">-- Select Subject ID --</option>
<?php
$strSQL = "SELECT DISTINCT sub_id FROM subject ORDER BY sub_id ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?php echo $objResult["sub_id"];?>"><?php echo $objResult["sub_id"];?></option>
<?php
}
?>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
<label for="subname" class=".col-xs-4 "> Subject Name : </label>
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<select class="form-control input-lg" name="subname" OnChange="Listsec(this.value);">
<option value="0">-- Select Subject --</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
<label for="subsec" class=".col-xs-4 "> Section : </label>
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<select class="form-control input-lg" name="subsec">
<option value="0">-- Select Section --</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
<label for="year" class=".col-xs-4 "> Year : </label></div>
<div class="col-xs-6 col-sm-4">
<input type="" class="form-control input-lg" name="year" autocomplete="off">
</div>
<div class="col-xs-6 col-sm-4">
<span>Ex: 1/2558</span>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-6 col-md-4">
<button type="submit" class="btn btn-info" value="submit">Save</button></div>
<div class="col-xs-6 col-md-4">
<button type="reset" class="btn btn-warning" value="reset">Cancel</button></div>
<div class="col-xs-6 col-md-4">
<button type="reset" class="btn btn-danger" value="cancel"onclick="window.location='Home.php';">Back</button></div>
</div>
</form>
</div>
</div>
<!-- Javascript -->
<script src="assets/js/jquery-1.11.1.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/jquery.backstretch.min.js"></script>
<script src="assets/js/scripts.js"></script>
<!--[if lt IE 10]>
<script src="assets/js/placeholder.js"></script>
<![endif]-->
</body>
</html>
<?php
mysql_close($objConnect);
?>
เอาโค้ดจาดสองกระทู้นี้มาลองเขียนแล้ว รหัสวิชาขึ้นแค่อย่างเดียวแต่ชื่อวิชากับหมู่เรียนไม่ขึ้น
http://www.thaicreate.com/php/forum/061522.html
http://www.thaicreate.com/community/dependant-listmenu-dropdownlist.html
ลองใช้ AJAX ดีไหมครับ
<select name="level1" id="level1" onChange="getL1(this.value);">
<option value="">Select L1</option>
<?php
foreach($results as $l1) {
?>
<option value="<?php echo $l1["id"]; ?>"><?php echo $l1["name"]; ?></option>
<?php
}
?>
</select>
<select name="level2" id="level2" onChange="getL2(this.value);">
<option value="">Select L2</option>
</select>
<select name="level3" id="level3">
<option value="">Select L3</option>
</select>
<script>
function getL1(val) {
$.ajax({
type: "POST",
url: "get_l1.php",
data:'id='+val,
success: function(data){
$("#level2").html(data);
}
});
}
function getL2(val) {
$.ajax({
type: "POST",
url: "get_l2.php",
data:'id='+val,
success: function(data){
$("#level3").html(data);
}
});
}
</script>
get_l1.php
if(!empty($_POST["id"])) {
$query ="SELECT * FROM xxx WHERE sub_id = '" . $_POST["id"] . "'";
$results = mysql_fetch_array(mysql_query($query));
?>
<option value="">Select L2</option>
<?php
foreach($results as $l2) {
?>
<option value="<?php echo $l2["id"]; ?>"><?php echo $l2["name"]; ?></option>
<?php
}
}
get_l2.php
if(!empty($_POST["id"])) {
$query ="SELECT * FROM xxx WHERE sub_id = '" . $_POST["id"] . "'";
$results = mysql_fetch_array(mysql_query($query));
?>
<option value="">Select L3</option>
<?php
foreach($results as $l2) {
?>
<option value="<?php echo $l2["id"]; ?>"><?php echo $l2["name"]; ?></option>
<?php
}
}
ตอบ/อธิบาย