以下是一个Ajax例子:
<!DOCTYPE html>
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Demo</title>
<script type="text/javascript" language="JavaScript">
function ajax(){
if( window.XMLHttpRequest ){
this.request = new XMLHttpRequest(); //
}else{
this.request = new ActiveXObject("Microsoft.XMLHTTP");
}
this.request.open( 'POST','ajax.PHP',true );
// this.request.open( 'GET','ajax.PHP?r='+document.getElementById('r').value+'&&r2='+document.getElementById('r2').value,true );
this.request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
this.request.send('r='+document.getElementById('r').value+'&&r2='+document.getElementById('r2').value);
var obj = this.request;
this.request.onreadystatechange = function(){
if( obj.readyState >= 4 ){
if( obj.status == 200 ){
document.getElementById('div').innerHTML = obj.responseText;
}
}
}
}
</script>
</head>
<body>
<div id="div" style="border:1px solid red; width:100%; height:300px;">这里要显示什么内容呢?</div>
姓名:<input type="text" name="r" id="r" /><br/>
地址:<input type="text" name="r2" id="r2" />
<input type="button" value="提交" onClick="ajax()" />
</body>
</html>
ajax.PHP
<?PHP header("Content-type: text/html; charset=utf-8"); //print_r($_POST); echo '姓名:'.$_POST['r'].'<br/>'; echo '年龄:'.$_POST['r2'].'<br/>'; echo '时间:'.date("Y-m-d H:i:s",time() );
原文链接:https://www.f2er.com/ajax/165037.html