Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to add Autocomplete searchbox in PHP MySQL using JQuery?

 


index.php

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Hancie e-Learning Studio</title>
<!-- Script -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!-- jQuery UI -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs
/jqueryui/1.12.1/jquery-ui.css" />
<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="term" id="term" placeholder="
search here...." class="form-control">  
  </div>
</div>

<script type="text/javascript">
  $(function() {
     $( "#term" ).autocomplete({
       source: 'result.php',
     });
  });
</script>

</body>
</html>

result.php

<?php
require_once "connection.php";
if (isset($_GET['term'])) {
     
   $query = "SELECT * FROM tea_bill 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);
}
?>

connection.php

<?php
$sernamename="localhost";
$username="root";
$password="";
$dbname="tea";

$conn=new mysqli($sernamename, $username, $password, $dbname);

if(!$conn){
    die ("Connection fail" .mysqli_error());

}


?>


Output

Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.