|
 |
|
Dropdown List Dependent ListMenu เอาโค้ดนำมาประยุกต์ใช้แล้ว มันไม่แสดงผลครับ |
|
 |
|
|
 |
 |
|
จากบทเรียนนี้ >>> https://www.thaicreate.com/community/dependant-listmenu-dropdownlist.html
ผมได้นำมาประยุกต์โค้ดใหม่เป็นดังนี้
dropdown (PHP)
<?php require_once('connection/connect.php'); ?>S
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language = "JavaScript">
function ListDepartment(SelectValue)
{
form1.department.length = 0
//*** Insert null Default Value ***//
var myOption = new Option('','')
form1.department.options[form1.department.length]= myOption
<?
$intRows = 0;
$sql = "SELECT * FROM chapters ORDER BY chapterId ASC ";
$result = mysql_query($sql) or die ("Error Query [".$sql."]");
$intRows = 0;
while($row = mysql_fetch_array($result))
{
$intRows++;
?>
x = <?=$intRows;?>;
mySubList = new Array();
strGroup = <?=$row["courseId"];?>;
strValue = "<?=$row["chapterId"];?>";
strItem = "<?=$row["chapterName"];?>";
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])
form1.department.options[form1.department.length]= myOption
}
<?
}
?>
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<select id="faculty" name="faculty" onChange = "ListDepartment(this.value)">
<option selected value="">Select</option>
<?
$sql = "SELECT * FROM courses ORDER BY courseId ASC ";
$result = mysql_query($sql) or die ("Error Query [".$sql."]");
while($row = mysql_fetch_array($result))
{
?>
<option value="<?=$row["courseId"];?>"><?=$row["courseName"];?></option>
<?
}
?>
</select>
<select id="department" name="department">
<option>Select</option>
</select>
</form>
</body>
</html>
ผมเคยนำมาประยุกต์ใช้ว่า dropdown ตัวแรกคือ คณะ หากเราเลือกคณะวิศวะ dropdown ตัวที่สองจะส่งผลว่าคณะวิศวะมีภาควิชาอะไรบ้าง โดยใน database มีตารางคณะหนึ่งตารางและภาควิชาหนึ่งตาราง ซึ่งมี PK เป็นtype number ซึ่งผมนำมาใช้แต่ก็สำเร็จครับ
แต่พอผมจะใช้กับ courses ซึ่งเป็นตารางเก็บวิชาเรียน โดยมี PK เป็น char(6) เช่น ABC123
ดรอบดาวน์ของผมเป็นวิชา พอเลือกอันที่สองจะต้องขึ้นว่า บทchapterของวิชาที่เลือกจากวิชาอันแรกมมีอะไรมั่ง
แต่พอเอามาใช้ ดรอบดาวน์แรกขึ้น แต่อันที่สองมันไม่ขึ้นตามอันแรกครับ ไม่รู้เพราะอะไร แต่ผมสงสัยว่าอาจจะเป็นที่ courseId ของผมมันเป็น char น่ะครับ รบกวนทีครับ ต้องใช้ด้วย ขอบคุณครับ
Tag : PHP, MySQL, JavaScript

|
|
 |
 |
 |
 |
Date :
2010-09-07 18:45:15 |
By :
mawinoma |
View :
1760 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้ามันไม่แสดงผลเป็นไปได้คือ ตอนที่ส่งค่า value ไปเชคใน array ค่าไม่ตรงกันครับ
|
 |
 |
 |
 |
Date :
2010-09-07 22:11:58 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีปัญหาเหมือนกันค่ะ ลอง alert ค่า strGroup strValue strItem แล้วก็ไม่ได้ด้วยไม่รุเป็นเพราะอะไร
ใครช่วยที
|
 |
 |
 |
 |
Date :
2010-09-14 16:03:20 |
By :
ีueng |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เฮ้ เราหาเจอแล้วท่านอิอิ
strGroup = <?=$row["courseId"];?>;
มันอยู่้ตรงนี้นี่เอง เปลี่ยนเป็น
strGroup = "<?=$row["courseId"];?>";
ก็ได้แล้วค่ะ ไม่รุจะเหมือนกัน
|
 |
 |
 |
 |
Date :
2010-09-14 17:16:37 |
By :
ueng |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันเป็น string เลย error ครับ แต่ถ้าเป็น number จะไม่มีปัญหาครับ
|
 |
 |
 |
 |
Date :
2010-09-14 17:28:07 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
 
|
 |
 |
 |
 |
Date :
2010-09-14 18:02:40 |
By :
lhynarm |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|