首页 > 开发 > PHP > 正文

ThinkPHP实现ajax仿官网搜索功能实例

2024-05-04 22:17:00
字体:
来源:转载
供稿:网友

本文实例讲述了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); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表