AJAX 实时读取输入文本(php)
程序员文章站
2022-03-14 10:40:03
客户端代码: 复制代码 代码如下:
客户端代码:
<!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);
?>
复制代码 代码如下:
<!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);
?>
上一篇: 一个有趣的Ajax Hack示范