程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> ThinkPHP實現ajax仿官網搜索功能實例

ThinkPHP實現ajax仿官網搜索功能實例

編輯:PHP綜合

本文實例講述了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框架程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved