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" οnchange="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页面中。