Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Autocomplete Search Box in PHP MySQL - Php Coding Stuff</title>
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
- <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
- <!-- Bootstrap Css -->
- <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
- </head>
- <body>
- <div class="container">
- <div class="row">
- <h2>Search Here</h2>
- <input type="text" name="search" id="search" placeholder="search here...." class="form-control">
- </div>
- </div>
- <script type="text/javascript">
- $(function() {
- $( "#search" ).autocomplete({
- source: 'ajax-db-search.php',
- });
- });
- </script>
- </body>
- </html>
- another page ajax-db-search.php
- <?php
- require_once "db.php";
- if (isset($_GET['term'])) {
- $query = "SELECT * FROM users WHERE name LIKE '{$_GET['term']}%' LIMIT 25";
- $result = mysqli_query($conn, $query);
- if (mysqli_num_rows($result) > 0) {
- while ($user = mysqli_fetch_array($result)) {
- $res[] = $user['name'];
- }
- } else {
- $res = array();
- }
- //return json res
- echo json_encode($res);
- }
- ?>
- reference url: https://stackoverflow.com/questions/41104478/php-mysql-autocomplete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement