Chinaunix首页 | 论坛 | 博客
  • 博客访问: 572569
  • 博文数量: 107
  • 博客积分: 5065
  • 博客等级: 大校
  • 技术积分: 1362
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-29 16:01
文章存档

2009年(84)

2008年(23)

我的朋友

分类:

2009-01-14 21:47:19

找来的代码都是可运行, 如果有问题就请指出. 以方面大家或新手用.
 
ajax.html
程序代码 CODE:
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>兼容多浏览器的AJAX入门实例(超详细注释)</title>
<script type="text/javascript">
<!--
//Ajax是建立在XMLHttp组件下的技术,本例详细语法参考压缩包内xmlhttp手册
var xmlHttp
//建立XMLHTTP对象调用MS的ActiveXObject方法,如果成功(IE浏览器)则使用MS ActiveX实例化创建一个XMLHTTP对象 非IE则转用建立一个本地Javascript对象的XMLHttp对象(此方法确保不同浏览器下对AJAX的支持)
function createXMLHttp(){
if(window.XMLHttpRequest){ // Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if (window.ActiveXObject){ // IE 浏览器
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
//建立主过程
function startXMLHttp(){
createXMLHttp(); //建立xmlHttp 对象
var send_string="name="+document.getElementById("name").value;
send_string= encodeURI(send_string)
// alert(document.getElementById("text").value);
// return;
xmlHttp.onreadystatechange =dodo; //xmlHttp下的onreadystatechange方法控制传送过程
xmlHttp.open("post","ajax_show.php",true); //传送方式 读取的页面 异步与否
// xmlHttp.setRequestHeader("cache-control","no-cache");
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(send_string); //发送
}
function dodo(){
if(xmlHttp.readystate==4){ // xmlHttp下的readystate方法 4表示传送完毕
if(xmlHttp.status==200){ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等
document.getElementById("content").innerHTML=xmlHttp.responseText //xmlHttp的responseText方法 得到读取页数据
}
}
}
-->
</script>
</head>
<body>
<span id="content">要替换的内容</span><br>
<input type="button" onclick="javascript:startXMLHttp()" value="AJAX获取"/>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="text" name="name" id="name" />
</label>
</form>
</body>
</html>

ajax_show.php
程序代码 :

<?php
$content = isset($_POST['name']) ? $_POST['name'] : '';
echo $content;
?>

阅读(2222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~