Home>Article>Backend Development> PHP combined with AJAX to implement search prompt function

PHP combined with AJAX to implement search prompt function

Guanhui
Guanhui Original
2020-05-20 17:32:50 2605browse

PHP combined with AJAX to implement search prompt function

PHP combined with AJAX to implement the search prompt function

First create an input box; then listen to the keyup event of the input box, when the event is triggered , use AJAX request to send the input content to PHP; then PHP searches for data based on the input content and returns it; finally, the returned data is rendered.

Sample code

Client:

Server :

true));//如果需要数据库长连接,需要最后加一个参数 } catch(PDOException $e){ exit('数据库连接失败'); } return $connect; } function test($keyword){//从数据库中查找关键字的函数 $db=connectDb(); $result=$db->prepare("select 字段名 from 数据库名字 where 字段名 like ?"); $result->bindParam(1,$keyword);//第一个问号的值 $result=>execute; return $result->fetchAll(PDO::FETCH_ASSOC); } $keyword=$_GET['keyword'];//获取输入框的内容 $suggestion=test($keyword); echo json_encode($suggestion);//输出查询的结果(json格式输出) ?>

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of PHP combined with AJAX to implement search prompt function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Basic operations of linux Next article:Basic operations of linux