Home > Article > Backend Development > Rating and comment implementation method developed in PHP in WeChat mini program
With the popularity and development of WeChat mini programs, more and more companies and developers have begun to use WeChat mini programs to build their own applications, and one of the essential functions is the rating and comment function. This article will introduce the implementation method of ratings and comments developed in WeChat applet using PHP.
First of all, we need to clarify the implementation principles of the two functions of rating and commenting. The rating function can be understood as a user rating a product or service and recording the score for reference by other users. The comment function allows users to leave their own comments and suggestions on a product or service page for other users’ reference.
Next, we need to clarify what technical tools are needed to implement these two functions. PHP is a popular server-side programming language widely used for web development. In the WeChat applet, we can use PHP to interact with the MySQL database to implement the rating and comment functions.
1. Implement the scoring function
The basis of the scoring function is to design a scoring control on the front-end page, which contains multiple scoring items and submit buttons. When the user clicks the submit button, the front end will send the user's rating data to the background PHP file through a POST request, and the background PHP file stores the data through the MySQL database. The code example is as follows:
Front-end page code (WXML):
<view class="rate"> <view class="title">服务评分:</view> <view class="stars"> <view wx:for="{{[1,2,3,4,5]}}" wx:key="{{index}}" class="star" data-score="{{index+1}}" bindtap="onStarClick"> <image src="{{index<score ? '/images/star_on.png' : '/images/star_off.png'}}"></image> </view> </view> <view wx:if="{{score>0}}" class="submit" bindtap="onSubmitClick">提交评分</view> </view>
Front-end logic code (JS):
Page({ data: { score: 0, }, onStarClick: function(e) { var score = e.currentTarget.dataset.score; this.setData({ score: score, }); }, onSubmitClick: function(e) { wx.request({ url: 'https://www.example.com/rate.php', data: { score: this.data.score, }, method: 'POST', success: function(res) { wx.showToast({ title: '评分成功', icon: 'success', }); }, fail: function(res) { wx.showToast({ title: '评分失败', icon: 'none', }); }, }); }, });
Backend PHP code:
<?php $score = $_POST['score']; if (!empty($score)) { $conn = mysqli_connect('localhost', 'user', 'password', 'database'); mysqli_query($conn, "INSERT INTO ratings (score) VALUES ('$score')"); } ?>
二, Implement the comment function
The comment function requires designing an input box and submit button on the front-end page. When the submit button is clicked, the front-end sends the user's comment data to the background PHP file through a POST request. The background PHP file Store data through MySQL database. In addition, in order to prevent malicious comments and protect user privacy, we need to filter and encrypt comment content. Code examples are as follows:
Front-end page code (WXML):
<view class="comment"> <textarea placeholder="写下你的评价" bindinput="onInput"></textarea> <view wx:if="{{content!=''}}" class="submit" bindtap="onSubmitClick">提交评价</view> </view>
Front-end logic code (JS):
Page({ data: { content: '', }, onInput: function(e) { var content = e.detail.value; this.setData({ content: content, }); }, onSubmitClick: function(e) { wx.request({ url: 'https://www.example.com/comment.php', data: { content: this.data.content, }, method: 'POST', success: function(res) { wx.showToast({ title: '评论成功', icon: 'success', }); }, fail: function(res) { wx.showToast({ title: '评论失败', icon: 'none', }); }, }); }, });
Backend PHP code:
<?php $content = $_POST['content']; if (!empty($content)) { $content = htmlspecialchars($content); // 过滤HTML标签 $content = addslashes($content); // 转义特殊字符 $conn = mysqli_connect('localhost', 'user', 'password', 'database'); $now = date('Y-m-d H:i:s'); // 获取当前时间 mysqli_query($conn, "INSERT INTO comments (content,time) VALUES ('$content','$now')"); } ?>
Summary
Through the introduction of this article, we learned how to use PHP to develop rating and comment functions in WeChat mini programs. Ratings and comments are one of the key factors in measuring user experience. It is very necessary for enterprises and developers to master this implementation method. At the same time, in order to improve user experience and data security, we also need to further optimize and upgrade the rating and comment functions in conjunction with other technical means.
The above is the detailed content of Rating and comment implementation method developed in PHP in WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!