本文實例講述了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; }); }); });
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
ThinkPHP3.1新特性對Ajax的支持更加完善的介紹
關于ThinkPHP通過AJAX返回JSON的方法
以上就是ThinkPHP實現ajax仿網址搜索的功能的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答