Thinkphp to realize Ajax Imitation website search function instance _php instance

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.