您的位置:首页 > 其它

使用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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐