首页 > 代码库 > AJAX 实时读取输入文本(php)

AJAX 实时读取输入文本(php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>无标题文档</title> 
</head> 
<script language="javascript"> 
var xmlHttp; 

function createXMLHttpRequest(){ 
if(window.ActiveXObject){ 
xmlHttp = new ActiveXObject("microsoft.XMLHTTP"); 
} 
else if(window.XMLHttpRequest){ 
xmlHttp = new XMLHttpRequest(); 
} 

} 

function sendRequest(){ 

createXMLHttpRequest(); 

var name = document.getElementById("name").value; 
url = "String_check.php?page="+name; 
xmlHttp.onreadystatechange = callback; 
xmlHttp.open(GET,url,true); 
xmlHttp.send(null); 
} 

function callback(){ 
if(xmlHttp.readyState == 4){ 
if(xmlHttp.status == 200){ 
document.getElementById("show").innerHTML = "您输入的字符串为:"+ xmlHttp.responseText; 

} 
} 
} 

</script> 
<body> 
<p>AJAX test;</p> 
<p><br/> 
<input type="text" id="name" onkeyup="sendRequest();" /> 
<br/> 
<span id="show"></span> 
</p> 
</body> 
</html> 


服务器端代码: 
复制代码 代码如下:

<?php 
header(‘Content-type: text/html;charset=GB2312‘); 
$ename = $_GET["page"]; 
if ($ename == "") { 
$ename = "请输入:"; 
} 
echo ($ename); 
exit(0); 
?> 

 

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399315233&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao12345%2Fp%2F3709106.html&dt=1399315237035&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740&correlator=1399315236875&frm=20&ga_vid=429972749.1397695120&ga_sid=1399310453&ga_hid=1475507969&ga_fc=1&u_tz=480&u_his=171&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=0&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=S64BUvgxGr&p=http%3A//www.cnblogs.com&dtd=43" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399315233&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao12345%2Fp%2F3709106.html&dt=1399315237086&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740%2C8660799060&correlator=1399315236875&frm=20&ga_vid=429972749.1397695120&ga_sid=1399310453&ga_hid=1475507969&ga_fc=1&u_tz=480&u_his=171&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=250&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=qWZz1dGlzs&p=http%3A//www.cnblogs.com&dtd=43" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>