• We just launched and are currently in beta. Join us as we build and grow the community.

Autocomplete with Suggestion Input using PHP, Vue.js, and MYSQL Database Tutorial

Xorcism

Tech Ecosystem Builder
X Rep
0
0
0
Rep
0
X Vouches
0
0
0
Vouches
0
Posts
71
Likes
38
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Introduction

In this tutorial, you will learn how to create an Autocomplete with suggestions input using PHP, Vue (Vue.js), and MySQL Database. The tutorial aims to provide IT/CS students or new programmers a reference for improving their web applications' client-side functionalities using Vue. Here, snippets are provided and a sample source code that demonstrates the main goal of this tutorial is also provided and is free to download.

What is Autocomplete with Suggestions Input?

In web applications, inputs with autocomplete with suggestions are commonly part of it to give the en-users a better experience while using the application and make it more efficient. This kind of input is mostly used for search boxes and input that queries options.

How to Autocomplete with Suggestions Input using Vue, PHP, and MySQL?

Autocomplete with Suggestions Input using Vue, PHP, and MySQL can be achieved in a few JavaScript and PHP lines of code. Like my past posted tutorial entitled (Live Search using PHP and Vue.JS Tutorial), suggestions data will be retrieved using the Fetch API and list it below the input.

Database

For this tutorial, let's assume that we have a database named dummy_db and have a MySQL Schema and table data using the snippet below.

  1. CREATE

    TABLE

    `languages`

    (
  2. `id`

    int

    (

    30

    )

    NOT

    NULL

    ,
  3. `name`

    varchar

    (

    250

    )

    NOT

    NULL


  4. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;

  5. INSERT

    INTO

    `languages`

    (

    `id`

    ,

    `name`

    )

    VALUES


  6. (

    1

    ,

    'PHP Programming'

    )

    ,
  7. (

    2

    ,

    'HTML Programming'

    )

    ,
  8. (

    3

    ,

    'CSS Programming'

    )

    ,
  9. (

    4

    ,

    'JavaScript Programming'

    )

    ,
  10. (

    5

    ,

    'Python Programming'

    )

    ,
  11. (

    6

    ,

    'C++ Programming'

    )

    ,
  12. (

    7

    ,

    'C# Programming'

    )

    ,
  13. (

    8

    ,

    '.NET Programming'

    )

    ,
  14. (

    9

    ,

    'VB Programming'

    )

    ,
  15. (

    10

    ,

    'ASP.NET Programming'

    )

    ,
  16. (

    11

    ,

    'Java Programming'

    )

    ;

  17. ALTER

    TABLE

    `languages`
  18. ADD

    PRIMARY KEY

    (

    `id`

    )

    ;

  19. ALTER

    TABLE

    `languages`
  20. MODIFY

    `id`

    int

    (

    30

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    12

    ;

Database Connection

Create a PHP file in your source code folder and save it as db-connect.php. This file contains the PHP Script for connecting the application to the database. See the following snippet.

  1. <?php
  2. $host

    =

    "localhost"

    ;
  3. $username

    =

    "root"

    ;
  4. $pw

    =

    ""

    ;
  5. $db_name

    =

    "dummy_db"

    ;

  6. $conn

    =

    new

    mysqli(

    $host

    ,

    $username

    ,

    $pw

    ,

    $db_name

    )

    ;

  7. if

    (

    !

    $conn

    )

    {
  8. die

    (

    "Database Connection Failed"

    )

    ;
  9. }

    <?php
  10. $host

    =

    "localhost"

    ;
  11. $username

    =

    "root"

    ;
  12. $pw

    =

    ""

    ;
  13. $db_name

    =

    "dummy_db"

    ;

  14. $conn

    =

    new

    mysqli(

    $host

    ,

    $username

    ,

    $pw

    ,

    $db_name

    )

    ;

  15. if

    (

    !

    $conn

    )

    {
  16. die

    (

    "Database Connection Failed"

    )

    ;
  17. }

Suggestions Query Script

Next, create another PHP File and save it as getLanguages.php. This file contains the PHP code that queries the suggested languages/data from the database table. This is a simple PHP API where the Fetch API will request the suggested data.

  1. <?php
  2. require_once

    (

    'db-connect.php'

    )

    ;
  3. /**
  4. * Search Language where name is like the search term
  5. */
  6. $swhere

    =

    ""

    ;

  7. if

    (

    isset

    (

    $_POST

    [

    'search'

    ]

    )

    &&

    !

    empty

    (

    $_POST

    [

    'search'

    ]

    )

    )

    {
  8. $swhere

    =

    " WHERE `name` LIKE '%{$_POST['search']}

    %' "

    ;
  9. }
  10. $sql

    =

    "SELECT * FROM `languages` {$swhere}

    order by `name` asc"

    ;
  11. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  12. $languages

    =

    [

    ]

    ;
  13. if

    (

    $query

    ->

    num_rows

    >

    0

    )

    {
  14. $languages

    =

    array_column(

    $query

    ->

    fetch_all

    (

    MYSQLI_ASSOC)

    ,

    'name'

    )

    ;
  15. }

  16. echo

    json_encode

    (

    $languages

    )

    ;
  17. $conn

    ->

    close

    (

    )

    ;

Interface

Next, create the page interface where you will display your autocomplete with suggestions input. The snippet I provided below uses CDNs for loading some external libraries for the design and Vue. Make sure to be connected to the internet when using the snippet to run it properly.

index.php

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Auto Complete | PHP & Vue.JS</title>
  8. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  9. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js" integrity="sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  11. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
  12. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  13. <style>
  14. html, body{
  15. min-height:100%;
  16. width:100%;
  17. }
  18. tbody:empty:after{
  19. content:'No records found'
  20. }
  21. #suggestion-field>.position-absolute {
  22. height: 30vh;
  23. overflow: auto;
  24. }
  25. </

    style

    >
  26. </

    head

    >
  27. <body

    >
  28. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    >
  29. <div

    class

    =

    "container"

    >
  30. <a

    class

    =

    "navbar-brand"

    href

    =

    "./"

    >

    Auto Complete</

    a

    >
  31. <div

    >
  32. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "text-light fw-bolder h6 text-decoration-none"

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  33. </

    div

    >
  34. </

    div

    >
  35. </

    nav>
  36. <div

    class

    =

    "container-fluid px-5 my-3"

    id

    =

    "SampleApp"

    >
  37. <div

    class

    =

    "col-lg-6 col-md-8 col-sm-12 mx-auto"

    >
  38. <h3

    class

    =

    "text-center"

    ><b

    >

    Auto Complete using PHP and Vue.js</

    b

    ></

    h3

    >
  39. <div

    class

    =

    "d-flex w-100 justify-content-center"

    >
  40. <hr

    class

    =

    "w-50"

    >
  41. </

    div

    >
  42. <div

    class

    =

    "card rounded-0 shadow mb-3"

    >
  43. <div

    class

    =

    "card-body"

    >
  44. <div

    class

    =

    "container-fluid"

    >
  45. <div

    class

    =

    "mb-3"

    >
  46. <div

    class

    =

    "input-group flex-nowrap"

    >
  47. <input

    type

    =

    "search"

    class

    =

    "form-control rounded-0"

    placeholder=

    "Search Prgramming Laguage"

    aria-label

    =

    "Search"

    aria-describedby=

    "addon-wrapping"

    v-model=

    "search"

    @input=

    "searchLanguages()"

    >
  48. <span

    class

    =

    "input-group-text rounded-0"

    id

    =

    "addon-wrapping"

    ><i

    class

    =

    "fa-solid fa-search"

    ></

    i

    ></

    span

    >
  49. </

    div

    >
  50. <div

    id

    =

    "suggestion-field"

    class

    =

    "position-relative w-100"

    v-show=

    "suggestions"

    >
  51. <div

    class

    =

    "position-absolute w-100 border"

    >
  52. <div

    id

    =

    "suggestion-lis"

    class

    =

    "list-group"

    >
  53. <a

    class

    =

    "list-group-item list-group-item-action"

    href

    =

    "#"

    v-for

    =

    "language in languages"

    @click=

    "getLanguage(language)"

    >
  54. {{language}}
  55. </

    a

    >
  56. <div

    class

    =

    "list-group-item text-center text-muted"

    v-if=

    "languages.length <= 0"

    ><i

    >

    No suggestion found</

    i

    ></

    div

    >
  57. </

    div

    >
  58. </

    div

    >
  59. </

    div

    >
  60. </

    div

    >
  61. </

    div

    >
  62. </

    div

    >
  63. </

    div

    >
  64. </

    div

    >
  65. </

    div

    >
  66. <script

    src

    =

    "app.js"

    ></

    script

    >
  67. </

    body

    >
  68. </

    html

    >

JavaScript

Lastly, create a new file and save it as app.js. The file contains the JavaScript codes that initiate the autocomplete with suggestions input using VueJS. It contains the method of retrieving the suggestions and selecting the input data.

  1. const

    {

    createApp }

    =

    Vue

  2. /**
  3. * Create Vue App to the selected Element
  4. */
  5. var

    app =

    createApp(

    {
  6. /**
  7. * App Data
  8. */
  9. data(

    )

    {
  10. return

    {
  11. languages:

    [

    ]

    ,
  12. search:

    ''

    ,
  13. suggestions:

    false
  14. }

  15. }

    ,
  16. /**
  17. * App Methods
  18. */
  19. methods:

    {
  20. searchLanguages(

    )

    {
  21. /**
  22. * Query Suggestions
  23. */
  24. if

    (

    this

    .search

    ==

    ""

    )

    {
  25. this

    .languages

    =

    [

    ]
  26. this

    .suggestions

    =

    false
  27. }

    else

    {
  28. var

    formData =

    new

    FormData(

    )

    ;
  29. formData.append

    (

    'search'

    ,

    this

    .search

    )
  30. fetch(

    'getLanguages.php'

    ,

    {
  31. method:

    'POST'

    ,
  32. body:

    formData
  33. }

    )
  34. .then

    (

    response =>

    {
  35. return

    response.json

    (

    )
  36. }

    )
  37. .then

    (

    data =>

    {
  38. this

    .languages

    =

    Object

    .values

    (

    data)
  39. this

    .suggestions

    =

    true
  40. }

    )
  41. }

  42. }

    ,
  43. getLanguage(

    language)

    {
  44. /**
  45. * Write selected suggestion to the search input
  46. */
  47. this

    .search

    =

    language
  48. this

    .languages

    =

    [

    ]
  49. this

    .suggestions

    =

    false
  50. }
  51. }
  52. }

    )

    .mount

    (

    '#SampleApp'

    )

That's it! the suggestion list will be triggered upon inputting in the provided text field. The selected suggestion will be automatically written on the input when clicking the item.

Snapshots

The snippets that I have provided above will result in the following snapshots.

Main Page Interface

Autocomplete with Suggestions Input

I have provided also the source code zip file that I created for this tutorial. You can download it by clicking the Download Button below this article. Feel free to download and modify it the way you wanted.

That's the end of this tutorial. I hope this Autocomplete with Suggestions Input Tutorial using PHP, VueJS, and MySQL will help you with what you are looking for and that you'll find this useful for your current and future projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding:)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

332,845

332,853

Top