Home > Article > Backend Development > How to use PHP to develop the voting function of WeChat applet?
How to use PHP to develop the voting function of WeChat mini program?
WeChat Mini Program is a very popular platform, and many people like to vote on WeChat Mini Program. PHP, as a popular server-side programming language, can be used in conjunction with WeChat mini-programs to implement voting functions. This article will introduce how to use PHP to develop the voting function of WeChat applet and provide specific code examples.
1. Create a database
First, we need to create a database to store voting information. Databases such as MySQL and SQLite can be used. Suppose we create a database named vote and create a table named options in it to store the options and the number of votes. The structure of the options table is as follows:
CREATE TABLE `options` ( `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, `option_name` varchar(255) NOT NULL, `votes` int(11) NOT NULL DEFAULT '0' );
2. Write the backend API
Next, we need to write the backend API to handle the request of the WeChat applet. Let's say we put all our APIs in a file called api.php. First, we need to connect to the database and set the character set:
<?php // 连接到数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "vote"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 设置字符集 mysqli_set_charset($conn, "utf8"); ?>
Next, we can start writing the specific API. The first is the API for obtaining voting options:
<?php // 获取投票选项 function getOptions() { global $conn; $sql = "SELECT * FROM options"; $result = $conn->query($sql); $options = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $option = array( "id" => $row["id"], "name" => $row["option_name"], "votes" => $row["votes"] ); array_push($options, $option); } } return $options; } // 处理请求 if ($_SERVER['REQUEST_METHOD'] === 'GET') { $result = getOptions(); echo json_encode($result); } ?>
Then the API for the voting function:
<?php // 投票 function vote($optionId) { global $conn; // 首先检查选项是否存在 $sql = "SELECT * FROM options WHERE id = $optionId"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 更新投票数量 $sql = "UPDATE options SET votes = votes + 1 WHERE id = $optionId"; $conn->query($sql); return true; } else { return false; } } // 处理请求 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $data = json_decode(file_get_contents('php://input'), true); $optionId = $data['optionId']; $result = vote($optionId); echo json_encode($result); } ?>
3. Call the API in the WeChat applet
In the WeChat applet, We can use wx.request API to send requests to the backend. Suppose we have a voting button on the mini program page. After clicking the button, a voting request will be sent. The code is as follows:
// 小程序代码 Page({ vote: function(optionId) { wx.request({ url: 'https://your_server_url/api.php', method: 'POST', data: { optionId: optionId }, success: function (res) { console.log(res.data); if (res.data) { wx.showToast({ title: '投票成功' }); } else { wx.showToast({ title: '投票失败' }); } } }); } });
In the above code, replace your_server_url with your server address.
Summary
This article introduces how to use PHP to develop the voting function of WeChat applet and provides specific code examples. Through the above steps, you can easily implement the voting function on the WeChat mini program. Hope this article helps you!
The above is the detailed content of How to use PHP to develop the voting function of WeChat applet?. For more information, please follow other related articles on the PHP Chinese website!