ThinkPHP中ajax仿官网搜索功能
后台代码:
-
- function search(){
- $keyword = $_POST['search'];
- $Goods=M('goods');
-
-
- $map['goods_id|goods_name'] = array('like','%'.$keyword.'%');
-
- if($goods=$Goods->where($map)->select())
- {
- $this->ajaxReturn($goods,'查询成功!',1);
- }else{
- $this->ajaxReturn($data,"查询失败,数据不存在!",0);
- }
- }
前端代码:
- $(document).ready(function(){
- $(".show_message").hide();
- var $search=$('#search_box');
- $("#submit_from").click(function(){
- if($("#search_box").attr("value")=='')
- {
-
- $(".show_message").html('错误提示:搜索框文本不能为空!');
- $(".show_message").fadeIn(1000);
- $(".show_message").fadeOut(1000);
- $search.focus();
-
- }else{
-
- $.ajax({
- type: "POST",
- url:"/index.php/Goods/search",
- data:{
- search:$search.val()
- },
- dataType: "json",
- success: function (data) {
- if (data.status == 1) {
-
- 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);
-
- }
- else if (data.status == 0) {
- $(".show_message").show();
- $(".show_message").html(data.info);
- $(".show_message").fadeOut(3000);
-
- return false;
- }
- }
- });
- }
- });
- });
|