ThinkPHP implements an example of ajax-like official website search function, thinkphpajax
The example in this article describes how ThinkPHP implements ajax-like official website search function. Share it with everyone for your reference.
The specific implementation method is as follows:
Backend code:
Copy code The code is as follows:
//Search, if it is 1 and not 0
function search(){
$keyword = $_POST['search'];
$Goods=M('goods');
//Here I made a fuzzy query to find the name or corresponding id. The main purpose is because my system is
// Used in the mall system to directly view the product ID
$map['goods_id|goods_name'] = array('like','%'.$keyword.'%');
// Pass the query conditions into the query method
If($goods=$Goods->where($map)->select())
{
$this->ajaxReturn($goods,'Query successful!',1);
}else{
$this->ajaxReturn($data,"Query failed, data does not exist!",0);
}
}
Front-end code:
Copy code The 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 message: The 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='';
through
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 programming.
http://www.bkjia.com/PHPjc/920606.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/920606.html
TechArticleThinkPHP implements an example of ajax imitating the official website search function, thinkphpajax This example describes how ThinkPHP implements the ajax imitation official website search function. Share it with everyone for your reference. Specific implementation method...