Thinkphp implements Ajax Imitation official website search function instance, Thinkphpajax
In this paper, we describe the method of thinkphp realizing Ajax imitation official website search function. Share to everyone for your reference.
The implementation method is as follows:
Background code:
Copy CodeThe code is as follows://Search, if at 1 not 0
function Search () {
$keyword = $_post[' search '];
$Goods =m (' Goods ');
Here I do a fuzzy query to the name or the corresponding ID, the main purpose because I this system is
Mall system used to directly look at the product ID
$map [' goods_id|goods_name '] = array (' Like ', '% '. $keyword. ' %');
Passing query criteria into Query method
if ($goods = $Goods->where ($map)->select ())
{
$this->ajaxreturn ($goods, ' query succeeded! ', 1);
}else{
$this->ajaxreturn ($data, "Query failed, data does not exist! ", 0);
}
}
Front-End code:
Copy CodeThe code is 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 hint: 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;
}
}
});
}
});
});
It is hoped that this article will be helpful to everyone's thinkphp framework design.
http://www.bkjia.com/PHPjc/920606.html www.bkjia.com true http://www.bkjia.com/PHPjc/920606.html techarticle Thinkphp realizes Ajax imitation official website search function instance, Thinkphpajax This article describes the thinkphp realization Ajax Imitation official website search function method. Share to everyone for your reference. Concrete implementation of the party ...