這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)ajax仿官網(wǎng)搜索功能的方法,實(shí)例演示了后臺(tái)查詢功能與前臺(tái)Ajax提交搜索數(shù)據(jù)的方法,是非常實(shí)用的技巧,需要的朋友可以參考下
本文實(shí)例講述了ThinkPHP實(shí)現(xiàn)ajax仿官網(wǎng)搜索功能的方法。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
后臺(tái)代碼:
復(fù)制代碼代碼如下:
//搜索,如果在1不在0
function search(){
$keyword = $_POST['search'];
$Goods=M('goods');
//這里我做的一個(gè)模糊查詢到名字或者對(duì)應(yīng)的id,主要目的因?yàn)槲疫@個(gè)系統(tǒng)是
//商城系統(tǒng)里面用到直接看產(chǎn)品ID
$map['goods_id|goods_name'] = array('like','%'.$keyword.'%');
// 把查詢條件傳入查詢方法
if($goods=$Goods->where($map)->select())
{
$this->ajaxReturn($goods,'查詢成功!',1);
}else{
$this->ajaxReturn($data,"查詢失敗,數(shù)據(jù)不存在!",0);
}
}
前端代碼: 復(fù)制代碼代碼如下:
$(document).ready(function(){
$(".show_message").hide();
var $search=$('#search_box');
$("#submit_from").click(function(){
if($("#search_box").attr("value")=='')
{
//alert('請(qǐng)輸入文字!');
$(".show_message").html('錯(cuò)誤提示:搜索框文本不能為空!');
$(".show_message").fadeIn(1000);
$(".show_message").fadeOut(1000);
$search.focus();
//return false;
}else{
//開始ajax執(zhí)行數(shù)據(jù)
$.ajax({
type: "POST",
url:"/index.php/Goods/search",
data:{
search:$search.val()
},
dataType: "json",
success: function (data) {
if (data.status == 1) {
//alert(data.info);
var html='';
$.each(data.data,function(no,items){
html+='';
});
html+="
'+items.goods_id+' '+items.goods_name+' '+items.add_time+' '+items.brand+' '+items.price+'";
$(".goods-list").html(' ').html(html);
// alert(html);
}
else if (data.status == 0) {
$(".show_message").show();
$(".show_message").html(data.info);
$(".show_message").fadeOut(3000);
// alert(data.info);
return false;
}
}
});
}
});
});
希望本文所述對(duì)大家的ThinkPHP框架程序設(shè)計(jì)有所幫助。