With the rapid development of the Internet, the user experience of Web applications has become an increasingly important factor. Using Ajax technology to implement the like function is a common way. This article will introduce how to use the ThinkPHP framework to quickly implement an Ajax-based like function.
1. Development environment preparation
This article uses the ThinkPHP5.1 framework. You need to install PHP5.5 or above and a MySQL database, and ensure that the environment can run ThinkPHP.
2. Create database table
Create the following table in MySQL:
CREATE?TABLE?`likes`?( ??`id`?int(11)?NOT?NULL?AUTO_INCREMENT, ??`content_id`?int(11)?NOT?NULL?COMMENT?'點贊的文章id', ??`user_id`?int(11)?NOT?NULL?COMMENT?'點贊的用戶id', ??`created_time`?int(11)?NOT?NULL?DEFAULT?'0'?COMMENT?'點贊時間戳', ??`updated_time`?int(11)?NOT?NULL?DEFAULT?'0'?COMMENT?'更新時間戳', ??PRIMARY?KEY?(`id`) )?ENGINE=InnoDB?DEFAULT?CHARSET=utf8mb4;
3. Controller layer
Create a LikesController.php controller, use The following code obtains the Ajax request:
namespace?app\index\controller; use?think\Controller; use?think\Db; class?LikesController?extends?Controller { ????public?function?like() ????{ ????????$content_id?=?input('post.content_id'); ????????$user_id?=?input('post.user_id'); ????????$created_time?=?time(); ????????$updated_time?=?time(); ????????$data?=?[ ????????????'content_id'?=>?$content_id, ????????????'user_id'?=>?$user_id, ????????????'created_time'?=>?$created_time, ????????????'updated_time'?=>?$updated_time, ????????]; ????????$result?=?Db::name('likes')->insert($data); ????????if?($result)?{ ????????????return?json(['code'?=>?200,?'msg'?=>?'點贊成功']); ????????}?else?{ ????????????return?json(['code'?=>?500,?'msg'?=>?'點贊失敗']); ????????} ????} }
4. View layer
Create an index.html front-end page, use jQuery to monitor user click events, and send Ajax requests to the server:
<!DOCTYPE html> <html> <head> ????<title>點贊</title> ????<meta charset="utf-8"> ????<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> </head> <body> ????<div id="like_button" data-content-id="1" data-user-id="1">點贊</div> </body> <script type="text/javascript"> ????$(document).ready(function?()?{ ????????$('#like_button').click(function?()?{ ????????????var?content_id?=?$(this).data('content-id'); ????????????var?user_id?=?$(this).data('user-id'); ????????????$.ajax({ ????????????????url:?"/LikesController/like", ????????????????type:?"POST", ????????????????dataType:?"json", ????????????????data:?{"content_id":?content_id,?"user_id":?user_id}, ????????????????success:?function?(data)?{ ????????????????????if?(data.code?==?200)?{ ????????????????????????alert(data.msg); ????????????????????}?else?{ ????????????????????????alert(data.msg); ????????????????????} ????????????????} ????????????}); ????????}); ????}); </script> </html>
5. Routing settings
Add a route in the routing file (route.php):
Route::post('/LikesController/like',?'index/LikesController/like');
6. Test
Start the server and visit http://localhost/ index/index/index, click Like to test this function. Check whether records are added in the likes table in MySQL to ensure that the likes are successful.
7. Summary
By using the ThinkPHP framework and jQuery technology, an Ajax-based like function is implemented. This feature can improve the user experience of web applications and enhance the interaction between users and web applications.
The above is the detailed content of thinkphp quickly implements a like function based on Ajax. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)
