Home > Backend Development > PHP Tutorial > ThinkPHP implements ajax imitation official website search function

ThinkPHP implements ajax imitation official website search function

不言
Release: 2023-04-02 09:50:01
Original
1515 people have browsed it

This article mainly introduces the method of ThinkPHP to implement ajax-like official website search function. The example demonstrates the background query function and the method of submitting search data through front-end Ajax. It is a very practical skill. Friends who need it can refer to it

The example in this article describes how ThinkPHP implements the ajax imitation official website search function. Share it with everyone for your reference.

The specific implementation method is as follows:

Backend code:

//搜索,如果在1不在0  
function search(){  
    $keyword = $_POST['search'];  
    $Goods=M('goods');  
  //这里我做的一个模糊查询到名字或者对应的id,主要目的因为我这个系统是  
  //商城系统里面用到直接看产品ID  
    $map['goods_id|goods_name']  = array('like','%'.$keyword.'%');  
    // 把查询条件传入查询方法  
    if($goods=$Goods->where($map)->select())  
     {  
              $this->ajaxReturn($goods,'查询成功!',1);  
     }else{  
              $this->ajaxReturn($data,"查询失败,数据不存在!",0);  
 }  
}
Copy after login

Front-end code:

$(document).ready(function(){  
   $(".show_message").hide();  
   var $search=$('#search_box');  
   $("#submit_from").click(function(){  
    if($("#search_box").attr("value")=='')  
    {  
        //alert('请输入文字!');  
        $(".show_message").html('错误提示:搜索框文本不能为空!');  
        $(".show_message").fadeIn(1000);  
        $(".show_message").fadeOut(1000);  
        $search.focus();  
        //return false;  
    }else{  
        //开始ajax执行数据  
        $.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;  
        }  
      }  
         });  
    }  
  });  
});
Copy after login

and above That’s the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

A more complete introduction to the new features of ThinkPHP3.1 that support Ajax

##About ThinkPHP through AJAX Methods that return JSON

The above is the detailed content of ThinkPHP implements ajax imitation official website search function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template