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

Creating a Live Search in Table using Vue.js and PHP

socksucker

UX/UI Master
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
139
Likes
189
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial tackles on how to create a search function that searches MySQL

table using Vue.js

with the help of axios.js

and PHP

. In this tutorial, you can search for either the first name or the last name of members in the members' table then it will return members that match your search.

Getting Started

I've used CDN for Bootstrap, Vue.js, and Axios.js so, you need an internet connection for them to work. And also download XAMPP

Creating our Database

  • Open your XAMPP's Control Panel and start "Apache" and "MySQL"
  • Open PHPMyAdmin


  • Click databases, create a database and name it as vuetot


  • After creating a database, click the SQL

    and paste the below codes. See the image below for detailed instructions.

  1. CREATE

    TABLE

    `members`

    (
  2. `memid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `firstname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. `lastname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  5. PRIMARY KEY

    (

    `memid`

    )
  6. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

database_6_33.png


Inserting Data into our Database

Next, we insert sample data into our database to use in our search.

  1. Click vuetot

    database that we have created earlier.
  2. Click SQL

    and paste the following codes.
    1. INSERT

      INTO

      `members`

      (

      `memid`

      ,

      `firstname`

      ,

      `lastname`

      )

      VALUES


    2. (

      1

      ,

      'neovic'

      ,

      'devierte'

      )

      ,
    3. (

      2

      ,

      'gemalyn'

      ,

      'cepe'

      )

      ,
    4. (

      3

      ,

      'gemalyn'

      ,

      'devierte'

      )

      ,
    5. (

      4

      ,

      'julyn'

      ,

      'divinagracia'

      )

      ;

  3. Click Go button below.

Creating the Interface

This contains our sample table and our search input.

index.php


  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    Live Search using Vue.js with PHP</

    title

    >
  5. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    /

    >
  6. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.10/vue.min.js"

    ></

    script

    >
  7. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.min.js"

    ></

    script

    >
  8. </

    head

    >
  9. <body

    >
  10. <div

    id

    =

    "myapp"

    >
  11. <div

    class

    =

    "container"

    >
  12. <h1

    class

    =

    "page-header text-center"

    >

    Live Search using Vue.js with PHP</

    h1

    >
  13. <div

    class

    =

    "col-md-8 col-md-offset-2"

    >
  14. <div

    class

    =

    "row"

    >
  15. <div

    class

    =

    "col-md-4 col-md-offset-8"

    >
  16. <input

    type

    =

    "text"

    class

    =

    "form-control"

    placeholder=

    "Search Firstname or Lastname"

    v-on:keyup=

    "searchMonitor"

    v-model=

    "search.keyword"

    >
  17. </

    div

    >
  18. </

    div

    >
  19. <div

    style

    =

    "height:5px;"

    ></

    div

    >
  20. <table

    class

    =

    "table table-bordered table-striped"

    >
  21. <thead

    >
  22. <th

    >

    Firstname</

    th

    >
  23. <th

    >

    Lastname</

    th

    >
  24. </

    thead

    >
  25. <tbody

    >

  26. <tr

    v-if=

    "noMember"

    >
  27. <td

    colspan

    =

    "2"

    align

    =

    "center"

    >

    No member match your search</

    td

    >
  28. </

    tr

    >

  29. <tr

    v-for

    =

    "member in members"

    >
  30. <td

    >

    {{ member.firstname }}</

    td

    >
  31. <td

    >

    {{ member.lastname }}</

    td

    >
  32. </

    tr

    >
  33. </

    tbody

    >
  34. </

    table

    >
  35. </

    div

    >
  36. </

    div

    >
  37. </

    div

    >
  38. <script

    src

    =

    "app.js"

    ></

    script

    >
  39. </

    body

    >
  40. </

    html

    >

Creating a Script

This contains our vue.js code.

app.js


  1. var

    app =

    new

    Vue(

    {
  2. el:

    '#myapp'

    ,
  3. data:

    {
  4. members:

    [

    ]

    ,
  5. search:

    {

    keyword:

    ''

    }

    ,
  6. noMember:

    false
  7. }

    ,

  8. mounted:

    function

    (

    )

    {
  9. this

    .fetchMembers

    (

    )

    ;
  10. }

    ,

  11. methods:

    {
  12. searchMonitor:

    function

    (

    )

    {
  13. var

    keyword =

    app.toFormData

    (

    app.search

    )

    ;
  14. axios.post

    (

    'action.php?action=search'

    ,

    keyword)
  15. .then

    (

    function

    (

    response)

    {
  16. app.members

    =

    response.data

    .members

    ;

  17. if

    (

    response.data

    .members

    ==

    ''

    )

    {
  18. app.noMember

    =

    true

    ;
  19. }
  20. else

    {
  21. app.noMember

    =

    false

    ;
  22. }
  23. }

    )

    ;
  24. }

    ,

  25. fetchMembers:

    function

    (

    )

    {
  26. axios.post

    (

    'action.php'

    )
  27. .then

    (

    function

    (

    response)

    {
  28. app.members

    =

    response.data

    .members

    ;
  29. }

    )

    ;
  30. }

    ,

  31. toFormData:

    function

    (

    obj)

    {
  32. var

    form_data =

    new

    FormData(

    )

    ;
  33. for

    (

    var

    key in

    obj)

    {
  34. form_data.append

    (

    key,

    obj[

    key]

    )

    ;
  35. }
  36. return

    form_data;
  37. }

    ,

  38. }
  39. }

    )

    ;

Creating the API

Lastly, this contains our PHP

code in fetching member data from our database.

action.php


  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "vuetot"

    )

    ;

  3. if

    (

    $conn

    ->

    connect_error

    )

    {
  4. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  5. }

  6. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  7. $action

    =

    "show"

    ;

  8. if

    (

    isset

    (

    $_GET

    [

    'action'

    ]

    )

    )

    {
  9. $action

    =

    $_GET

    [

    'action'

    ]

    ;
  10. }

  11. if

    (

    $action

    ==

    'show'

    )

    {
  12. $sql

    =

    "select * from members"

    ;
  13. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  14. $members

    =

    array

    (

    )

    ;

  15. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  16. array_push

    (

    $members

    ,

    $row

    )

    ;
  17. }

  18. $out

    [

    'members'

    ]

    =

    $members

    ;
  19. }

  20. if

    (

    $action

    ==

    'search'

    )

    {
  21. $keyword

    =

    $_POST

    [

    'keyword'

    ]

    ;
  22. $sql

    =

    "select * from members where firstname like '%$keyword

    %' o

    r lastname like '%$keyword

    %'"

    ;
  23. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  24. $members

    =

    array

    (

    )

    ;

  25. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  26. array_push

    (

    $members

    ,

    $row

    )

    ;
  27. }

  28. $out

    [

    'members'

    ]

    =

    $members

    ;
  29. }

  30. $conn

    ->

    close

    (

    )

    ;

  31. header

    (

    "Content-type: application/json"

    )

    ;
  32. echo

    json_encode

    (

    $out

    )

    ;
  33. die

    (

    )

    ;

  34. ?>

DEMO

That ends this tutorial. I help you with what you are looking for and you'll find this tutorial useful for your future web application projects.

Happy Coding :)


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

452,292

324,125

324,133

Top