Home > Article > Backend Development > Implementation method of text box auto-complete function developed in PHP in WeChat applet
With the popularity of WeChat mini programs, various development needs are also increasing. Among them, the text box auto-complete function is one of the commonly used functions in mini programs. Although the WeChat applet provides some native components, some special needs still require secondary development. This article will introduce how to use PHP language to implement the automatic completion function of text boxes in WeChat applet.
Before starting development, you need to prepare some basic environments and tools. First, you need to install the PHP environment. Secondly, you need to obtain your AppID and AppSecret in the WeChat applet background. Finally, there needs to be a keyword data source that can be read.
To implement the automatic completion function of the text box, you need to follow the following steps:
(1) Get the text entered by the user;
(2) Match the text entered by the user with the keyword data source;
(3) Return the matching result to the front end;
(4) The front end returns The results show a drop-down list.
In order to implement these functions, you need to first write a PHP file to match and return results.
3.1 Code framework
f1ab33e16497b9eed930e4ad4189a3ce
3.2 Obtain keyword data source and parse
The keyword data source can be stored in a txt file and read and parsed through the following methods Analysis:
// Open the keyword data source file and read the keyword content
$file = fopen('keyword.txt', 'r');
$keywords = array() ;
while (!feof($file)) {
array_push($keywords,fgets($file));
}
fclose($file);
// Parse keyword data source
$keywords = array_map('trim', $keywords);
$keywords = array_filter($keywords);
$keywords = array_unique($keywords);
3.3 Keyword matching
Match the text entered by the user with the keyword and return the matching result:
foreach ($keywords as $keyword) {
// Use PHP Use the stripos function to match
if (stripos($keyword, $input) !== false) {
// 如果匹配成功,则将匹配结果存入$result数组中 array_push($result, $keyword);
}
}
3.4 Return matching results
Finally, return the matching result to the front-end in JSON format:
echo json_encode($result);
The front-end needs to pass The request function of the WeChat applet API calls the above PHP file and displays the return result in the drop-down list:
// 1. Define the bindinput event of the input input box
ba7bc17597fec4aa6e84203c6ae8a4fc
// 2. Define the onInput event processing function
onInput(e) {
const input = e. detail.value
// Send request to PHP file
wx.request({
url: 'http://localhost/autocomplete.php?input='+input, success: (res) => { const result = res.data // 接收并处理PHP文件返回的结果 this.setData({result}) }
})
}
// 3. Display the drop-down list on the interface
ccd0d47425b97f3c7b34639cb6cf9dc5
19150ee37db954dd2436e082547a6194{{item}}20908554640865bb724c429685f97dbe
de5f4c1163741e920c998275338d29b2
This article introduces how to use PHP language to implement the automatic completion function of text boxes in WeChat applet. Through the above steps, we can easily complete this function. At the same time, we also need to pay attention to the format of the data source and the selection of the matching algorithm to ensure the accuracy and stability of the function.
The above is the detailed content of Implementation method of text box auto-complete function developed in PHP in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!