《PHP實戰(zhàn):ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能實例》要點:
本文介紹了PHP實戰(zhàn):ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能實例,希望對您有用。如果有疑問,可以聯(lián)系我們。
本文實例講述了ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能的辦法.分享給大家供大家參考.PHP編程
具體實現(xiàn)辦法如下:PHP編程
后臺代碼:
PHP編程
代碼如下:
//搜索,如果在1不在0?
function search(){?
??? $keyword = $_POST['search'];?
??? $Goods=M('goods');?
? //這里我做的一個模糊查詢到名字或者對應的id,主要目的因為我這個系統(tǒng)是?
? //商城系統(tǒng)里面用到直接看產(chǎn)品ID?
??? $map['goods_id|goods_name']? = array('like','%'.$keyword.'%');?
??? // 把查詢條件傳入查詢辦法?
??? if($goods=$Goods->where($map)->select())?
???? {?
????????????? $this->ajaxReturn($goods,'查詢成功!',1);?
???? }else{?
????????????? $this->ajaxReturn($data,"查詢失敗,數(shù)據(jù)不存在!",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執(zhí)行數(shù)據(jù)?
??????? $.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框架程序設計有所幫助.PHP編程
維易PHP培訓學院每天發(fā)布《PHP實戰(zhàn):ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能實例》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/13503.html