main.php
<script language="javascript" src="initajax.js"></script><!--InitAjax()-->
<script>
<!--
function getoption(select1,target)
{
if(select1.value!=0){
//select1是提交數據的來源的select菜單名
var url = "getoption.php?pid="+select1.value;//取得xml的url
//alert(url);
var ajax = InitAjax();
var i = 0;
ajax.open("GET", url, true);
ajax.onreadystatechange = function() {
//如果執行是狀態正常,那麼就把返回的內容賦值給指定的地方
if (ajax.readyState == 4 && ajax.status == 200) {
var obj = ajax.responseXML;
var properties = obj.getElementsByTagName("property");
var name,value;
target.options.length = 1;
for (var i=0,x=1;i<properties.length;i++,x++) {
name = properties[i].getElementsByTagName("name")[0].firstChild.nodeValue;
value = properties[i].getElementsByTagName("value")[0].firstChild.nodeValue;
target.options[x] = new Option();
target.options[x].text = name;
target.options[x].value = value;
}
}
}
ajax.send(null);
}
}
<form method="post" name="form" action="index.php">
<select name="level1" onchange="getoption(document.form.level1,document.form.level2);"/>
<option selected="ture" value="0">===選擇===</option>
<?
if ($nrows>0){
for ($i=0;$i<$nrows;$i++){
echo "<option value="{$results[ID][$i]}">{$results[NAME][$i]}</option>";
}
}
?>
</select>
<select name="level2" onchange="getoption(document.form.level2,document.form.level3);">
<option selected="ture" value="0">===選擇===</option>
</select>
iniajax.js:
function InitAjax()
{
var ajax=false;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = false;
}
}
if (!ajax && typeof XMLHttpRequest!=undefined) {
ajax = new XMLHttpRequest();
}
return ajax;
}
getoption.php:
<?php
if($pid=$_GET["pid"])
{
header("Content-type: text/xml;charset=GB2312");
echo "<?xml version="1.0" encoding="GB2312"?>";
include("./oracle.inc");
$sql="select * from AJAXTEST where PARENTID = $pid";
//echo $sql;
$stmt=ociparse($handle,$sql);
ociexecute($stmt);
ocicommit($handle);
ocilogoff();
$nrows=ocifetchstatement($stmt,$results);
echo "<properties>";
//echo "<row>{$nrows}</row>";
for ($i=0;$i<$nrows;$i++){
echo "<property>";
echo "<value>{$results[ID][$i]}</value>";
echo "<name>{$results[NAME][$i]}</name>";
echo "</property>";
}
echo "</properties>";
}
?>