使用MVC框架实现百度搜索建议
2013-03-14 19:06
405 查看
完成类似百度搜索建议
tpl文件:
<script>
function startAjax(obj){
var xhr;
if(window.ActiveXObject){
xhr=new ActiveXOject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
var url="index.php?c=user&a=baiduSuggest";
xhr.open("post",url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("val="+obj);
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
//alert(xhr.responseText);
var json = eval('('+xhr.responseText+')');
//alert(json[0].content);
var str='';
for(var i=0;i<json.length;i++){
str+="<span>"+json[i].content+"</span><br />";
document.getElementById("dv").style.display="block";
document.getElementById("dv").innerHTML=str;
}zv
}
}
}
}
</script>
<body onload="init()">
<center>
<h3>百度一次,你就知道</h3>
<table>
<tr>
<td>
<form action="#" method="post">
<input type="text" size="30" id="search" onkeyup="startAjax(this.value)" />
<div id="dv" align="left" style=" position:relative; background-color:#CCC; border:dashed #999"></div>
</td><td>
<input type="submit" value="搜索" size="10" />
</td>
</form>
</tr>
</table>
</center>
</body>
userController.class.php文件:
public function showInterfaceAction(){
$this->smarty->display('showInterface.tpl');
}
public function baiduSuggestAction(){
$data=$_REQUEST['val'];
$userModel=new userModel('localhost','root','','baidu');
$rows=$userModel->selectAll($data);
//var_dump($rows);
echo json_encode($rows);
}
userModel.class.php文件:
public function selectAll($data){
$sql=" select * from c where content like '{$data}%'";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_assoc($result))
{
$rows[]=$row;
}
return $rows;
}
tpl文件:
<script>
function startAjax(obj){
var xhr;
if(window.ActiveXObject){
xhr=new ActiveXOject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
var url="index.php?c=user&a=baiduSuggest";
xhr.open("post",url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("val="+obj);
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
//alert(xhr.responseText);
var json = eval('('+xhr.responseText+')');
//alert(json[0].content);
var str='';
for(var i=0;i<json.length;i++){
str+="<span>"+json[i].content+"</span><br />";
document.getElementById("dv").style.display="block";
document.getElementById("dv").innerHTML=str;
}zv
}
}
}
}
</script>
<body onload="init()">
<center>
<h3>百度一次,你就知道</h3>
<table>
<tr>
<td>
<form action="#" method="post">
<input type="text" size="30" id="search" onkeyup="startAjax(this.value)" />
<div id="dv" align="left" style=" position:relative; background-color:#CCC; border:dashed #999"></div>
</td><td>
<input type="submit" value="搜索" size="10" />
</td>
</form>
</tr>
</table>
</center>
</body>
userController.class.php文件:
public function showInterfaceAction(){
$this->smarty->display('showInterface.tpl');
}
public function baiduSuggestAction(){
$data=$_REQUEST['val'];
$userModel=new userModel('localhost','root','','baidu');
$rows=$userModel->selectAll($data);
//var_dump($rows);
echo json_encode($rows);
}
userModel.class.php文件:
public function selectAll($data){
$sql=" select * from c where content like '{$data}%'";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_assoc($result))
{
$rows[]=$row;
}
return $rows;
}
相关文章推荐
- .NET中使用js实现百度搜索下拉提示效果[不是局部刷新,呜呜。。]
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码】
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- ajax实现百度搜索建议
- 使用Bootrap和Vue实现仿百度搜索功能
- 使用jsonp跨域调用百度js实现搜索框智能提示(转)
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- 百度搜狗等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)
- 简单实现google百度搜索建议
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- asp.net mvc 使用 Autocomplete 实现类似百度,谷歌动态搜索条提示框。
- 百度搜索建议的实现
- 利用MVC框架和Ajax(完成类似百度搜索建议)
- .NET使用js制作百度搜索下拉提示效果(不是局部刷新)实现思路
- 百度搜狗等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)
- 百度搜狗等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)
- asp.net mvc 使用 Autocomplete 实现类似百度,谷歌动态搜索条提示框。
- .NET中使用js实现百度搜索下拉提示效果[不是局部刷新,呜呜。。]
- 使用Bootrap和Vue实现仿百度搜索功能
- 百度等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)