 |
สอบถามเรื่อง dropdown list โดยการ select dropdown ก่อนแล้วจึงมีข้อมูลของ dropdownอันที่สอง |
|
 |
|
|
 |
 |
|
ตัวอย่าง ลองดูครับ แต่ต้องไปแก้ที่ Form 2 หน่อยครับ
************* Form 1
<?php
$hostname = "localhost"; //ชื่อโฮสต์
$user = "root"; //ชื่อผู้ใช้
$password = ""; //รหัสผ่าน
$dbname = "test"; //ชื่อฐานข้อมูล
// เริ่มติดต่อฐานข้อมูล
mysql_connect($hostname, $user, $password) or die("ติดต่อฐานข้อมูลไม่ได้");
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Form1 </TITLE>
<script language="javascript">
function fncChange(_val){
if(_val != ""){
document.fmsched.target = "ifupdate";
document.fmsched.method = "post";
document.fmsched.action = "test2.php?id="+_val;
document.fmsched.submit();
}
}
</script>
</HEAD>
<BODY>
<table width="824" height="500">
<tr>
<td height="100" bgcolor="#0000FF"></td>
</tr>
<tr>
<td><div align="center">
<form action="" name="fmsched" method="post">
<p>
<label>ID Subject : </label>
<select id="subject" onChange="fncChange(this.value)">
<option value="nothing" selected>Select</option>
<?php
$sqlsch = "SELECT * FROM schedule";
$dbquerysch = mysql_query($sqlsch);
while($objResultsub = mysql_fetch_array($dbquerysub))
{
?>
<option value=<?=$objResultsub[0];?>><?=$objResultsub[0];?></option>
<?}?>
</select>
</p>
<p><label>ID Section : </label>
<select name="section" id="section">
<option value="nothing" selected>Select</option>
</select>
</p>
<p>
<label>Location : </label>
<input type="text" id="locsproom" name="locsproom">
</p>
<p>
<label>Room : </label>
<input type="text" id="room" name="room">
</p>
<p>
<input type="submit" id="submit" value="submit">
</p>
</form></div>
</td>
</tr>
</table>
<a href="Home_Menu.php"><b><font size="+2">Back</font></b></a>
<iframe name="ifupdate" width="800" height="300" style="display:none"></iframe>
</BODY>
</HTML>
************* Form 2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Form2 </TITLE>
</HEAD>
<BODY>
<?php
$hostname = "localhost"; //ชื่อโฮสต์
$user = "root"; //ชื่อผู้ใช้
$password = ""; //รหัสผ่าน
$dbname = "test"; //ชื่อฐานข้อมูล
// เริ่มติดต่อฐานข้อมูล
mysql_connect($hostname, $user, $password) or die("ติดต่อฐานข้อมูลไม่ได้");
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้");
$id = $_REQUEST["id"];
if($id != ""){
$arrValue = "";
$sqlsec = "SELECT * FROM section where id=".$id; // where case
$rs = mysql_query($sqlsec) or die ("Error Query [".$sql_code."]");
while ($objResult = mysql_fetch_array($rs)){
if($arrValue != "")$arrValue.= ",";
$arrValue .= $objResult["value"];
}
if($arrValue != ""){
?>
<script language="javascript">
var newElem;
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
var _arr="<?=$arrValue;?>";
var _arrValue = _arr.split(",");
var PChooser = parent.document.fmsched.elements["section"];
while (PChooser.options.length) {
PChooser.remove(0);
}
newElem = document.createElement("option");
newElem.text = "---select---";
newElem.value = "";
PChooser.add(newElem, where);
for (var i = 0; i < _arrValue.length; i++) {
newElem = document.createElement("option");
newElem.text = _arrValue[i];
newElem.value =_arrValue[i];
PChooser.add(newElem, where);
}
</script>
<?php
}
}
?>
</BODY>
</HTML>
|
 |
 |
 |
 |
Date :
2013-05-29 14:05:48 |
By :
thep |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|