与我们合作
我们专注:网站策划设计、网络多媒体传播、网站优化及网站营销、品牌策略与设计
主营业务:网站建设、移动端微信小程序开发、VI设计、网络运营、云产品·运维解决方案
有一个品牌项目想和我们谈谈吗?
您可以填写右边的表格,让我们了解您的项目需求,这是一个良好的开始,我们将会尽快与您取得联系。当然也欢迎您给我们写信或是打电话,让我们听到您的声音
您也可通过下列途径与我们取得联系:
地 址: 深圳.龙岗区大运软件小镇11栋3楼
电 话: 138 2888 4598 / 138 0880 9053
网 址: http://www.appvx.cn
快速提交您的需求 ↓
本文实例讲述了ThinkPHP实现ajax仿官网搜索功能的方法。分享给大家供大家参考。
具体实现方法如下:
后台代码:
代码如下:
//搜索,如果在1不在0
function search(){
$keyword = $_POST['search'];
$Goods=M('goods');
//这里我做的一个模糊查询到名字或者对应的id,主要目的因为我这个系统是
//商城系统里面用到直接看产品ID
$map['goods_id|goods_name'] = array('like','%'.$keyword.'%');
// 把查询条件传入查询方法
if($goods=$Goods->where($map)->select())
{
$this->ajaxReturn($goods,'查询成功!',1);
}else{
$this->ajaxReturn($data,"查询失败,数据不存在!",0);
}
}
前端代码:
代码如下:
$(document).ready(function(){
$(".show_message").hide();
var $search=$('#search_box');
$("#submit_from").click(function(){
if($("#search_box").attr("value")=='')
{
//alert('请输入文字!');
$(".show_message").html('错误提示:搜索框文本不能为空!');
$(".show_message").fadeIn(1000);
$(".show_message").fadeOut(1000);
$search.focus();
//return false;
}else{
//开始ajax执行数据
$.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;
}
}
});
}
});
});
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。