


Use ThinkPHP to implement ajax-like official website search function
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); } }
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; } } }); } }); });
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:
Analysis on ThinkPhp framework form validation and ajax validation issues
##Implementing ajax based on Thinkphp and jquery Function of deleting data by inverting multiple selections
The above is the detailed content of Use ThinkPHP to implement ajax-like official website search function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.
