Advertisement
Narendra123

autocomplete search

Feb 24th, 2021
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Autocomplete Search Box in PHP MySQL - Php Coding Stuff</title>
  6.  
  7. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
  8.  
  9. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  10. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  11. <!-- Bootstrap Css -->
  12. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="row">
  17. <h2>Search Here</h2>
  18. <input type="text" name="search" id="search" placeholder="search here...." class="form-control">
  19. </div>
  20. </div>
  21. <script type="text/javascript">
  22. $(function() {
  23. $( "#search" ).autocomplete({
  24. source: 'ajax-db-search.php',
  25. });
  26. });
  27. </script>
  28. </body>
  29. </html>
  30.  
  31.  
  32. another page ajax-db-search.php
  33.  
  34. <?php
  35. require_once "db.php";
  36. if (isset($_GET['term'])) {
  37.  
  38. $query = "SELECT * FROM users WHERE name LIKE '{$_GET['term']}%' LIMIT 25";
  39. $result = mysqli_query($conn, $query);
  40.  
  41. if (mysqli_num_rows($result) > 0) {
  42. while ($user = mysqli_fetch_array($result)) {
  43. $res[] = $user['name'];
  44. }
  45. } else {
  46. $res = array();
  47. }
  48. //return json res
  49. echo json_encode($res);
  50. }
  51. ?>
  52.  
  53.  
  54. reference url: https://stackoverflow.com/questions/41104478/php-mysql-autocomplete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement