//
原创作品
本站原创:www.111cn.cn
作者:面条爱兔子 QQ:271728967
注明:转载请说明原出去 http://www.111cn.cn
//
现在长沙下着大雪啊,晚上回家也没什么事作,白天在公司写一个BBS完成了一部份,突然昨天听一个网友说如果能实现google效果就好了,今天无聊之下就想了想,觉得这个用ajax做应该不是什么难道了,就试着写了,说句实话我学ajax时间很短,也只懂皮毛了,各位看了后别丢石头了,把钱包丢过来吧,过年没钱用,;)呵呵.好了废话就不多说了下面进行正题.
首先我总体的简介一下,我只用了两个文件了,因为是测试所以就不分那么清楚了,把js文件和html写在一个文件test.html里面了,还有一个就是php文件post.php了,这个文件用来处理ajax发送过来的数进行处理,再由ajax把数据返回给test.html里面的div, 原理不这么简单了,下面我们来看代码.
第一步创建数据表:test
CREATE TABLE `test` (
`id` int(4) NOT NULL auto_increment,
`title` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=5 ;
好了数据库创建成功了我们就来建立test.html文件,这个文件很简单,就是一个表单和一个div和CSS
上面为CSS了就是用来控制效果的,
This is the content above. Let’s explain the function in detail below.
The second step: processing and calling js and xmlhttp.
Create xmlhttp. I talked about this function last time when I talked about ajax+php imitating the window file manager. I also talked about it in user registration. I won’t go into it here. For the specific address, please go to:
var xmlHttp = false;
function ajaxcreate(){
try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != \'undefined\') {
xmlHttp = new XMLHttpRequest();
}
if(!xmlHttp){alert(\'Create Xmlhttp Fail \');return false;}
}
The function suggest() below is to obtain the value of the key and send it to the post file for processing, and then call the returnstate() function
function suggest(){
ajaxcreate();
var xmvalue=document.getElementById("key").value;
var url="post.php?key="+encodeURI(xmvalue)+"rnd="+Math.random();
if (xmvalue== null || xmvalue.length>20 || xmvalue == "") return false;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(xmvalue);
xmlHttp.onreadystatechange=returnstate;
}
The returnstate() function below is to determine whether the status of xmlhttp is waiting for 4. 4 means that the sending is successful. In fact, a 200 means that the reception is completed
function returnstate(){
if(xmlHttp.readyState != 4 ){
document.getElementById("sug").innerHTML="plase wait....";
}
if(xmlHttp.readyState == 4){
document.getElementById("sug").innerHTML=xmlHttp.responseText;
}
}
The following functions are just some basic processing, which I won’t go into.
function fillin(str){
document.getElementById(\'key\').value=str;
obj =document.getElementById(\'sug\');
obj.innerHTML=\'\';
obj.style.display=\'none\';
}
function other(){
document.getElementById(\'sug\').style.display=\'block\';
}
function losefouse(){
setInterval("func()",4000);
var time=setInterval("func()",1000);
clearTimeout(time);
}
function func(){
ob=document.getElementById(\'sug\');
ob.style.display = \'none\';
}
The last one is the post.php file, and then comes the next article.
php+ajax implements Google search function 2 [original]
Effect browsing address: http://www.111cn.cn/test/test.html