ThinkPHP實現ajax仿官網搜尋功能執行個體_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了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架構程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.