|
|
|
ช่วยอธิบาย Code List Menu 2 ชั้น (แบบดึงข้อมูลจาก Database) |
|
|
|
|
|
|
|
ได้แล้วแต่ยังไม่ค่อยเข้าใจ
config.php
<?php
$host = "localhost"; // ส่วนมากมักเป็น localhost
$user = "root"; // Username
$pass = "1234"; // Password
$dbname = "itservice"; // ชื่อฐานข้อมูล
function conndb() {
global $conn;
global $host;
global $user;
global $pass;
global $dbname;
$conn = mysql_connect($host,$user,$pass);
mysql_query("SET NAMES 'utf8'");
mysql_select_db($dbname) ;
if (!$conn)
die("ไม่สามารถติดต่อกับฐานข้อมูลได้");
mysql_select_db($dbname,$conn)
or die("ไม่สามารถเลือกใช้งานฐานข้อมูลได้");
}
function closedb() {
global $conn;
mysql_close($conn);
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Drop Down menu</title>
</head>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
try { return new XMLHttpRequest(); } catch(e) {}
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText;
}
}
};
req.open("GET", "localtion.php?data="+src+"&val="+val);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
req.send(null);
}
window.onLoad=dochange('Division', -1);
</script>
<body>
<h3>Drop Down menu</h3>
<form name="form" method="post" action="">
<p>
กอง :
<span id="Division">
<select>
<option value="0">- เลือก -</option>
</select>
</span>
</p>
<p>
แผนก :
<span id="SectionName">
<select>
<option value='0'>- เลือก -</option>
</select>
</span>
</p>
<input type="submit" name="Submit" value="ตกลง"> <INPUT type="reset" value="ยกเลิก">
</form>
</body>
</html>
localtion.php
<?php
header("content-type: text/html; charset=utf-8");
header ("Expires: Wed, 21 Aug 2013 13:13:13 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
include "config.php";
conndb();
$data = $_GET['data'];
$val = $_GET['val'];
if ($data=='Division') {
echo "<select name='Division' onChange=\"dochange('SectionName', this.value)\">";
echo "<option value='0'>- เลือก -</option>\n";
$result=mysql_query("select * from section order by Division");
while($row = mysql_fetch_array($result)){
echo "<option value='$row[SectionID]' >$row[Division]</option>" ;
}
}
else if($data=='SectionName') {
echo "<select name='SectionName'>\n";
echo "<option value='0'>- เลือก -</option>\n";
$result=mysql_query("SELECT * FROM section WHERE SectionID= '$val' ORDER BY SectionName");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[SectionID]\" >$row[SectionName]</option> " ;
}
}
echo "</select>\n";
echo mysql_error();
closedb();
?>
Tag : PHP, JavaScript, Ajax, jQuery, JAVA
|
ประวัติการแก้ไข 2015-04-09 21:30:16 2015-04-09 21:31:48 2015-04-09 21:34:40 2015-04-09 21:35:16
|
|
|
|
|
Date :
2015-04-09 19:26:31 |
By :
speedfine |
View :
2093 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
น่าจะใช้ jQuery แทนดีกว่าครับ
|
|
|
|
|
Date :
2015-04-10 00:01:50 |
By :
Krungsri |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jQuery ดีกว่ายังไงเหรอครับ
|
|
|
|
|
Date :
2015-04-10 02:04:36 |
By :
speedfine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|