This article is an example of the thinkphp to realize Ajax Imitation website search function. Share to everyone for your reference.
The implementation methods are as follows:
Background code:
Copy Code code as follows:
Search, if in 1 not 0
function Search () {
$keyword = $_post[' search '];
$Goods =m (' goods ');
Here I do a fuzzy query to the name or corresponding ID, the main purpose because I this system is
Mall system inside use to look directly at the Product ID
$map [' goods_id|goods_name '] = array (' Like ', '% '. $keyword. ') %');
Passing query criteria into the Query method
if ($goods = $Goods->where ($map)->select ())
{
$this->ajaxreturn ($goods, ' query success! ', 1);
}else{
$this->ajaxreturn ($data, "Query failed, data does not exist!" ", 0);
}
}
Front-End code:
Copy Code code as follows:
$ (document). Ready (function () {
$ (". Show_message"). Hide ();
var $search =$ (' #search_box ');
$ ("#submit_from"). Click (function () {
if ($ ("#search_box"). attr ("value") = = ")
{
Alert (' Please enter text! ');
$ (". Show_message"). HTML (' Error tip: Search box text cannot be empty! ');
$ (". Show_message"). FadeIn (1000);
$ (". Show_message"). fadeout (1000);
$search. focus ();
return false;
}else{
Start Ajax Execution data
$.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;
}
}
});
}
});
});
I hope this article will be helpful to everyone's thinkphp framework program design.