ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能實例
更新時間:2014年12月02日 14:35:59 投稿:shichen2014
這篇文章主要介紹了ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能的方法,實例演示了后臺查詢功能與前臺Ajax提交搜索數(shù)據(jù)的方法,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了ThinkPHP實現(xiàn)ajax仿官網(wǎng)搜索功能的方法。分享給大家供大家參考。
具體實現(xiàn)方法如下:
后臺代碼:
復(fù)制代碼 代碼如下:
//搜索,如果在1不在0
function search(){
$keyword = $_POST['search'];
$Goods=M('goods');
//這里我做的一個模糊查詢到名字或者對應(yīng)的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);
}
}
function search(){
$keyword = $_POST['search'];
$Goods=M('goods');
//這里我做的一個模糊查詢到名字或者對應(yīng)的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);
}
}
前端代碼:
復(fù)制代碼 代碼如下:
$(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;
}
}
});
}
});
});
$(".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框架程序設(shè)計有所幫助。
您可能感興趣的文章:
- TP5(thinkPHP5)框架基于ajax與后臺數(shù)據(jù)交互操作簡單示例
- ThinkPHP5.1+Ajax實現(xiàn)的無刷新分頁功能示例
- thinkPHP5 ajax提交表單操作實例分析
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- ThinkPHP結(jié)合AjaxFileUploader實現(xiàn)無刷新文件上傳的方法
- thinkphp中AJAX返回ajaxReturn()方法分析
- thinkphp中ajax與php響應(yīng)過程詳解
- ThinkPHP處理Ajax返回的方法
- thinkPHP+ajax實現(xiàn)統(tǒng)計頁面pv瀏覽量的方法
- thinkphp驗證碼的實現(xiàn)(form、ajax實現(xiàn)驗證)
- thinkPHP5框架實現(xiàn)基于ajax的分頁功能示例
- TP5(thinkPHP5)框架使用ajax實現(xiàn)與后臺數(shù)據(jù)交互的方法小結(jié)
相關(guān)文章
ThinkPHP實現(xiàn)非標準名稱數(shù)據(jù)表快速創(chuàng)建模型的方法
這篇文章主要介紹了ThinkPHP實現(xiàn)非標準名稱數(shù)據(jù)表快速創(chuàng)建模型的方法,對于采用ThinkPHP操作非標準名稱數(shù)據(jù)表的情況非常實用,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11Thinkphp框架使用list_to_tree 實現(xiàn)無限級分類列出所有節(jié)點示例
這篇文章主要介紹了Thinkphp框架使用list_to_tree 實現(xiàn)無限級分類列出所有節(jié)點,結(jié)合實例形式分析了thinkPHP遞歸遍歷與數(shù)據(jù)查詢、排序等相關(guān)操作技巧,需要的朋友可以參考下2020-04-04Laravel解決nesting level錯誤和隱藏index.php的問題
今天小編就為大家分享一篇Laravel解決nesting level錯誤和隱藏index.php的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10