PHP 8 Jquery Select2 Ajax Autocomplete Example

May 26, 2022 . Admin

Hello Friends,

This tutorial will give you example of PHP 8 Jquery Select2 Ajax Autocomplete Example. This post will give you simple example of Dynamically load data in Select2 with AJAX in PHP 8. I would like to show you PHP 8 MySQL Ajax Live Search Autocomplete Example. you will learn Dynamically Add Item to jQuery Select2 Control using Ajax with PHP 8 MySQL.

I will give you simple example of How to Create AJAX Autocomplete Search in PHP 8 with Select2.

So, let's see bellow solution:

<html lang="en">
    <title>PHP 8 Jquery Select2 Ajax Autocomplete Example</title>
    <link rel="stylesheet" href="" integrity="sha256-aAr2Zpq8MZ+YA/D6JtRD3xtrwpEz2IqOS+pWD/7XKIw=" crossorigin="anonymous" />
    <script src="" integrity="sha256-OFRAJNoaD8L3Br5lglV7VyLRf0itmoBzWUoM+Sji4/8=" crossorigin="anonymous"></script>
    <script src=""></script>
    <link href="" rel="stylesheet" />
    <script src=""></script>
    <div class="row mt-5">
        <div class="col-md-6 offset-3 mt-5">
            <div class="card">
                <div class="card-header bg-info text-center text-white">
                    <h6 class="m-0">PHP 8 Jquery Select2 Ajax Autocomplete Example -</h6>
                <div class="card-body" style="height: 280px;">
                        <select class="postName form-control" name="postName"></select>
    <script type="text/javascript">
            placeholder: 'Select a post',
            ajax: {
                url: '/autocompletePro.php',
                dataType: 'json',
                delay: 250,
                data: function (data) {
                    return {
                        searchTerm: data.term // search term
                processResults: function (response) {
                    return {
                cache: true
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "db_php";
    $mysqli = new mysqli($servername,$username,$password,$dbname);

        $json = [];
        $search = $_GET['searchTerm'];
        $sql = "SELECT * FROM post 
                WHERE title LIKE '%".$search."%'
                LIMIT 10"; 
        $result = $mysqli->query($sql);
        $json = [];
        while($row = $result->fetch_assoc()){
            $json[] = ['id'=>$row['id'], 'text'=>$row['title']];

    echo json_encode($json);


It will help you...
#PHP 8