ผมลองเอา code ของพี่วิน ตาม link https://www.thaicreate.com/community/dependant-listmenu-dropdownlist.html ไปใช้รวมกับ form ที่เตรียมไว้ครับ แต่พอ run บน browser มันไม่เป็นไปตามที่คาดสิครับ menu list มันแสดงผลแต่เฉพาะส่วนของ Geography แต่ Province กับ Amphur ไม่แสดงผลครับ ไม่รู้ code ไปผิดตรงไหน ผมเองยังไม่ค่อยแม่น code ด้วยสิ รบกวนผู้รู้มาแนะนำหน่อยครับ
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","nop") or die("Error Connect to Database");
$objDB = mysql_select_db("thailand");
@mysql_query("SET NAMES UTF8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Check username</title>
<style type="text/css">
<!--
.style1 {color: #0000FF}
-->
</style>
</head>
<body>
<script language = "JavaScript">
//**** List Province (Start) ***//
function ListProvince(SelectValue)
{
frmMain.ddlProvince.length = 0
frmMain.ddlAmphur.length = 0
//*** Insert null Default Value ***//
var myOption = new Option('','')
frmMain.ddlProvince.options[frmMain.ddlProvince.length]= myOption
<?
$intRows = 0;
$strSQL = "SELECT * FROM province ORDER BY PROVINCE_ID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
?>
x = <?=$intRows;?>;
mySubList = new Array();
strGroup = <?=$objResult["GEO_ID"];?>;
strValue = "<?=$objResult["PROVINCE_ID"];?>";
strItem = "<?=$objResult["PROVINCE_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])
frmMain.ddlProvince.options[frmMain.ddlProvince.length]= myOption
}
<?
}
?>
}
//**** List Province (End) ***//
//**** List Amphur (Start) ***//
function ListAmphur(SelectValue)
{
frmMain.ddlAmphur.length = 0
//*** Insert null Default Value ***//
var myOption = new Option('','')
frmMain.ddlAmphur.options[frmMain.ddlAmphur.length]= myOption
<?
$intRows = 0;
$strSQL = "SELECT * FROM amphur ORDER BY AMPHUR_ID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
?>
x = <?=$intRows;?>;
mySubList = new Array();
strGroup = <?=$objResult["PROVINCE_ID"];?>;
strValue = "<?=$objResult["AMPHUR_ID"];?>";
strItem = "<?=$objResult["AMPHUR_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])
frmMain.ddlAmphur.options[frmMain.ddlAmphur.length]= myOption
}
<?
}
?>
}
//**** List Amphur (End) ***//
</script>
<h1 class="style1">::Register::</h1>
<form action="register2.php" method="post" name="frmMain">
<table cellspacing = "2">
<tr>
<td><b>Username :</b></td>
<td><input name="user_reg" type="text" value="">*</td>
</tr>
<tr>
<td><b>Passwaord : </b></td>
<td><input name="pass_reg" type="password">*</td>
</tr>
<tr>
<td><b>ชื่อ-สกุล : </b></td>
<td><input name="name_reg" type="text">*</td>
</tr>
<tr>
<td><b>เพศ : </b></td>
<td><input name="sex_reg" type="radio" value="ชาย"
checked>
ชาย
<input name="sex_reg" type="radio" value+"หญิง">
หญิง</td>
</tr>
<tr>
<td><b>อีเมล :</b></td>
<td><input name="email_reg" type="text" size="26">*</td>
</tr>
<tr>
<td><b>โทรศัพท์ :</b></td>
<td><input name="tel_reg" type="text" size="26">*</td>
</tr>
<tr>
<td><b>ที่อยู่ติดต่อ :</b></td>
<td><input name="address_reg" cols="35" rows="3">
</textarea></td>
</tr>
<tr>
<td>Geography
</td>
<td><select id="ddlGeo" name="ddlGeo" onChange = "ListProvince(this.value)">
<option selected value=""></option>
<?
$strSQL = "SELECT * FROM geography ORDER BY GEO_ID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["GEO_ID"];?>"><?=$objResult["GEO_NAME"];?></option>
<?
}
?>
</select>
</td>
</tr>
<tr>
<td>Province</td>
<td><select id="ddlProvince" name="ddlProvince" style="width:120px" onChange = "ListAmphur(this.value)"></select>
</td>
</tr>
<tr>
<td>Amphur</td>
<td><select id="ddlAmphur" name="ddlAmphur" style="width:200px"></select></td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" value="Submit">
<input type="Reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
<?
mysql_close($objConnect);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">