<script language='JavaScript' type='text/JavaScript'>
function InitAjax()//初始化ajax對象
{
//return "adfasd";
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;
}
</script>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<select name="typeid" id="typeid" onchange="getchlidtype();">
<option value="-1">商戶類型</option>
<? foreach ($type as $v){?>
<option value="<?=$v[linkageid]?>"><?=$v['name']?></option>
<? }?>
</select></span><span id="show"></span>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<script>
function getchlidtype()
{
//需要進行Ajax的URL地址
var pid = document.getElementById("typeid");
var url = "http://192.168.0.102/youyuanhui_com/ajax/getoption.php?pid="+ pid.value;
//擷取新聞顯示層的位置
var show = document.getElementById("show");
//執行個體化Ajax對象
var ajax = InitAjax();
//使用Get方式進行請求
ajax.open("GET", url, true);
//擷取執行狀態
ajax.onreadystatechange = function() {
//如果執行是狀態正常,那麼就把返回的內容賦值給上面指定的層
if (ajax.readyState == 4 && ajax.status == 200) {
show.innerHTML = ajax.responseText;
}
}
//發送空
ajax.send(null);
}
</script>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
getoption.php
<?php
require '../include/common.inc.php';
require '../include/system_info.php';
global $db;
if($pid=$_GET["pid"])
{
$options=$db->select("select * from yyh_linkage where parentid=".$pid);
echo "<select name='typeid2' id='typeid2'>";
echo "<option value='-1'>-選擇子類</option>";
foreach($options as $opt)
{
echo "<option value=".$opt['linkageid'].">".$opt['name']."</option>";
}
echo "</select>";
}
?>
原理:假設A,B兩個頁面
A頁面是AJAX請求,在A頁面中要顯示資料資訊,則A通過ajax請求B頁面,B頁面在取得相應資料後顯示在B頁面上,則B頁面的內容就會返回到A頁面中。