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

Live Checking of Input Data Availability using VueJS and PHP Tutorial

Santaliz

SERP Spy
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
88
Likes
91
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you will learn how to implement a Live Checking of Input Data Availability using VueJS and PHP. This tutorial aims to provide IT students and new programmers a reference for implementing a live interaction of an application between the client and server side using the said JS Framework and Programming Language. I have provided some snippets below for this tutorial. A sample web application source code file that demonstrates the objective of this tutorial is also provided and free to download.

How does Live Checking of Input Data Availability work?

Live Checking of Input Data Availability is one of the effective features of a web application. Some developers prefer to implement this kind of feature in some of their project forms. It allows the end-users or the form encoders to be notified right away if the data entered on the specific text field that requires a unique value is already existing. With this, end-users will know the availability status of the data immediately even if the form is not submitted yet.

How to implement or create a Live Checking of Input Data Availability using VueJS and PHP?

The Live Checking of Input Data Availability feature can be achieved in so many ways using PHP and Vue. Here are the following methods, functions, properties, and APIs of PHP and Vue that can help in implementing the said feature.

JavaScript

  • Vue methods
  • JS Fetch API
  • Vue Models
  • JS FormData

PHP

  • PHP MySQL OOP
  • MySQL SELECT Statement

To get a better understanding of how to use the given methods, functions, properties, and APIs of PHP and Vue above to implement a Live Checking of Input Data Availability feature, check out the snippets that I provided below. The snippets output a simple web application that inserts new employee records into the database.

Snippets

Database

The snippet that I provided uses a MySQL Database named dummy_db. The database has the following MySQL Schema.

  1. CREATE

    TABLE

    `employee_

    list`

    (
  2. `id`

    int

    (

    30

    )

    NOT

    NULL

    Primary Key

    AUTO_INCREMENT

    ,
  3. `code`

    varchar

    (

    50

    )

    NOT

    NULL


  4. `name`

    varchar

    (

    250

    )

    NOT

    NULL


  5. `email`

    varchar

    (

    250

    )

    NOT

    NULL


  6. `department`

    varchar

    (

    250

    )

    NOT

    NULL


  7. `designation`

    varchar

    (

    250

    )

    NOT

    NULL


  8. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;

Database Connection

The following snippet is a PHP File named db-connect.php. The script handles the connection between the application and the database.

  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. }

Form Page Interface

The following snippet contains the script of the main page and the sample form interface. Save the file as 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

    >

    Check Input Data Availability | 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. </

    style

    >
  22. </

    head

    >
  23. <body

    >
  24. <nav class

    =

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

    >
  25. <div

    class

    =

    "container"

    >
  26. <a

    class

    =

    "navbar-brand"

    href

    =

    "./"

    >

    Check Input Data Availability</

    a

    >
  27. <div

    >
  28. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

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

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  29. </

    div

    >
  30. </

    div

    >
  31. </

    nav>
  32. <div

    class

    =

    "container-fluid px-5 my-3"

    id

    =

    "SampleApp"

    >
  33. <div

    class

    =

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

    >
  34. <h3

    class

    =

    "text-center"

    ><b

    >

    Live Checking of Input Data Availability using PHP and Vue</

    b

    ></

    h3

    >
  35. <div

    class

    =

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

    >
  36. <hr

    class

    =

    "w-50"

    >
  37. </

    div

    >
  38. <div

    class

    =

    "card rounded-0 shadow mb-3"

    >
  39. <div

    class

    =

    "card-body"

    >
  40. <div

    class

    =

    "container-fluid"

    >
  41. <div

    class

    =

    "alert alert-success rounded-0 mb-3"

    v-show=

    "is_success"

    >

    {{success_message}}</

    div

    >
  42. <div

    class

    =

    "alert alert-danger rounded-0 mb-3"

    v-show=

    "is_error"

    >

    {{error_message}}</

    div

    >

  43. <form

    action

    =

    ""

    id

    =

    "sample-form"

    @submit=

    "formSubmit"

    >
  44. <div

    class

    =

    "mb-3"

    >
  45. <label

    for

    =

    "code"

    class

    =

    "form-label"

    >

    Employee Code</

    label

    >
  46. <input

    type

    =

    "text"

    id

    =

    "code"

    name

    =

    "code"

    v-model=

    "code"

    @keyup=

    "checkAvailability('code')"

    maxLength

    =

    "50"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  47. <div

    class

    =

    "alert alert-danger rounded-0 mt-3"

    v-show=

    "input_error.code.status"

    >

    {{input_error.code.msg}}</

    div

    >
  48. <div

    class

    =

    "alert alert-success rounded-0 mt-3"

    v-show=

    "input_success.code.status"

    >

    {{input_success.code.msg}}</

    div

    >
  49. </

    div

    >
  50. <div

    class

    =

    "mb-3"

    >
  51. <label

    for

    =

    "name"

    class

    =

    "form-label"

    >

    Name</

    label

    >
  52. <input

    type

    =

    "text"

    id

    =

    "name"

    name

    =

    "name"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  53. </

    div

    >
  54. <div

    class

    =

    "mb-3"

    >
  55. <label

    for

    =

    "email"

    class

    =

    "form-label"

    >

    Email</

    label

    >
  56. <input

    type

    =

    "text"

    id

    =

    "email"

    name

    =

    "email"

    v-model=

    "email"

    @keyup=

    "checkAvailability('email')"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  57. <div

    class

    =

    "alert alert-danger rounded-0 mt-3"

    v-show=

    "input_error.email.status"

    >

    {{input_error.email.msg}}</

    div

    >
  58. <div

    class

    =

    "alert alert-success rounded-0 mt-3"

    v-show=

    "input_success.email.status"

    >

    {{input_success.email.msg}}</

    div

    >
  59. </

    div

    >
  60. <div

    class

    =

    "mb-3"

    >
  61. <label

    for

    =

    "department"

    class

    =

    "form-label"

    >

    Department</

    label

    >
  62. <input

    type

    =

    "text"

    id

    =

    "department"

    name

    =

    "department"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  63. </

    div

    >
  64. <div

    class

    =

    "mb-3"

    >
  65. <label

    for

    =

    "designation"

    class

    =

    "form-label"

    >

    Desgination</

    label

    >
  66. <input

    type

    =

    "text"

    id

    =

    "designation"

    name

    =

    "designation"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  67. </

    div

    >
  68. </

    form

    >
  69. </

    div

    >
  70. </

    div

    >
  71. <div

    class

    =

    "card-footer py-1 text-center"

    >
  72. <button

    class

    =

    "btn btn-primary rounded-pill col-lg-4 col-md-6 col-sm-12"

    form=

    "sample-form"

    >

    Save Employee</

    button

    >
  73. </

    div

    >
  74. </

    div

    >
  75. </

    div

    >
  76. </

    div

    >
  77. <script

    src

    =

    "app.js"

    ></

    script

    >
  78. </

    body

    >
  79. </

    html

    >

JavaScript

The snippet below is a JavaScript file that contains the script for initiating the VueJS. The methods of checking the input data availability and form submission are written in the snippet. Save it as app.js.

  1. const

    {

    createApp }

    =

    Vue

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

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

    )

    {
  10. return

    {
  11. is_error:

    false

    ,
  12. is_success:

    false

    ,
  13. success_message:

    ""

    ,
  14. error_message:

    ""

    ,
  15. code:

    ''

    ,
  16. email:

    ''

    ,
  17. input_error:

    {
  18. code:

    {

    status:

    false

    ,

    msg:

    ""

    }

    ,
  19. email:

    {

    status:

    false

    ,

    msg:

    ""

    }
  20. }

    ,
  21. input_success:

    {
  22. code:

    {

    status:

    false

    ,

    msg:

    ""

    }

    ,
  23. email:

    {

    status:

    false

    ,

    msg:

    ""

    }
  24. }
  25. }

  26. }

    ,
  27. /**
  28. * Application Methods
  29. */
  30. methods:

    {


  31. /**
  32. * Live Checking Input Data Availability Method
  33. * @param {string} input
  34. */
  35. checkAvailability(

    input)

    {
  36. if

    (

    this

    [

    input]

    ==

    ''

    ||

    this

    [

    input]

    ==

    null

    )

    {
  37. this

    .input_error

    [

    input]

    =

    {

    status:

    false

    ,

    msg:

    ""

    }
  38. this

    .input_success

    [

    input]

    =

    {

    status:

    false

    ,

    msg:

    ""

    }
  39. return

    false

    ;
  40. }

  41. //Setting Form Data
  42. var

    fdata =

    new

    FormData(

    )

    ;
  43. fdata.append

    (

    input,

    this

    [

    input]

    )
  44. fdata.append

    (

    "check_live"

    ,

    'true'

    )

  45. //Request for cheching input availability
  46. fetch(

    "checkAvailability.php"

    ,

    {
  47. method:

    'POST'

    ,
  48. body:

    fdata
  49. }

    )
  50. .then

    (

    response =>

    response.json

    (

    )

    )
  51. .then

    (

    data=>

    {
  52. if

    (

    !!

    data.status

    )

    {
  53. if

    (

    data.status

    ==

    'success'

    )

    {
  54. if

    (

    !!

    data.success

    )

    {
  55. //Return Success Message
  56. Object

    .keys

    (

    data.success

    )

    .map

    (

    k =>

    {
  57. this

    .input_success

    [

    k]

    =

    data.success

    [

    k]
  58. this

    .input_error

    [

    k]

    =

    {

    status:

    false

    ,

    msg:

    ''

    }
  59. }

    )
  60. }
  61. if

    (

    !!

    data.error

    )

    {
  62. //Return Error Message
  63. Object

    .keys

    (

    data.error

    )

    .map

    (

    k =>

    {
  64. this

    .input_error

    [

    k]

    =

    data.error

    [

    k]
  65. this

    .input_success

    [

    k]

    =

    {

    status:

    false

    ,

    msg:

    ''

    }
  66. }

    )
  67. }
  68. }
  69. }
  70. }

    )
  71. .catch

    (

    error=>

    {
  72. console.error

    (

    error)
  73. }

    )
  74. }

    ,
  75. formSubmit(

    e)

    {
  76. e.preventDefault

    (

    )

    ;

  77. //Submitted Form
  78. var

    form =

    e.target

  79. // Setting New Form Data
  80. var

    fdata =

    new

    FormData(

    form)

    ;

  81. // Chech some Availability Error before saving the record
  82. if

    (

    this

    .input_error

    .code

    .status

    ||

    this

    .input_error

    .email

    .status

    )

    {
  83. is_error =

    true
  84. error_message=

    "Some of the input data are already exists."
  85. }


  86. // Request for Saving the record
  87. fetch(

    "saveEmployee.php"

    ,

    {
  88. method:

    'POST'

    ,
  89. body:

    fdata
  90. }

    )
  91. .then

    (

    response =>

    response.json

    (

    )

    )
  92. .then

    (

    data=>

    {
  93. if

    (

    !!

    data.status

    )

    {
  94. if

    (

    data.status

    ==

    'success'

    )

    {
  95. // If the request process is successful
  96. form.reset

    (

    )

    ;
  97. this

    .is_success

    =

    true

    ;
  98. this

    .is_error

    =

    false

    ;
  99. this

    .code

    =

    ""

    ;
  100. this

    .email

    =

    ""

    ;
  101. if

    (

    !!

    data.msg

    )

    {
  102. this

    .success_message

    =

    data.msg
  103. }
  104. this

    .input_error

    =

    {
  105. code:

    {

    status:

    false

    ,

    msg:

    ""

    }

    ,
  106. email:

    {

    status:

    false

    ,

    msg:

    ""

    }
  107. }

    ,
  108. this

    .input_success

    =

    {
  109. code:

    {

    status:

    false

    ,

    msg:

    ""

    }

    ,
  110. email:

    {

    status:

    false

    ,

    msg:

    ""

    }
  111. }
  112. }

    else

    {
  113. // If the request process has failed to continue
  114. this

    .is_success

    =

    false

    ;
  115. if

    (

    !!

    data.error

    )

    {
  116. Object

    .keys

    (

    data.error

    )

    .map

    (

    k =>

    {
  117. this

    .input_success

    [

    k]

    =

    {

    status:

    false

    ,

    msg:

    ''

    }
  118. this

    .input_error

    [

    k]

    =

    data.error

    [

    k]
  119. }

    )
  120. }
  121. if

    (

    !!

    data.msg

    )

    {
  122. this

    .is_error

    =

    true

    ;
  123. this

    .error_message

    =

    data.msg
  124. }
  125. }
  126. }
  127. }

    )
  128. .catch

    (

    error=>

    {
  129. console.error

    (

    error)
  130. }

    )

  131. }
  132. }
  133. }

    )

    .mount

    (

    '#SampleApp'

    )

Check Availability API

Here is the snippet of a PHP File that contains the PHP Script for checking the availability of the entered input value of the end user. The script will be executed using the checkAvailability() method on the Vue App script. The script will be used also before the insertion process to check the availability again before executing the insertion of data on the database. Save it as checkAvailability.php.

  1. <?php
  2. include_once

    (

    'db-connect.php'

    )

    ;

  3. function

    check_availability(

    )

    {
  4. global

    $conn

    ;
  5. // Allowed fields to check
  6. $allowed_fields

    =

    [

    'code'

    ,

    'email'

    ]

    ;
  7. $response

    [

    'status'

    ]

    =

    'success'

    ;

  8. /**
  9. * Dynamically checking the input data availablity
  10. */
  11. foreach

    (

    $_POST

    as

    $k

    =>

    $v

    )

    {
  12. if

    (

    in_array

    (

    $k

    ,

    $allowed_fields

    )

    )

    {
  13. $v

    =

    $conn

    ->

    real_escape_string

    (

    trim

    (

    $v

    )

    )

    ;

  14. // SQL Statement
  15. $check_sql

    =

    "SELECT id FROM `employee_list` where `{$k}

    ` = '{$v}

    ' "

    ;
  16. // SQL Query
  17. $check_qry

    =

    $conn

    ->

    query

    (

    $check_sql

    )

    ;

  18. if

    (

    $check_qry

    ->

    num_rows

    >

    0

    )

    {
  19. // If data does not exists yet
  20. $response

    [

    'error'

    ]

    [

    $k

    ]

    [

    'status'

    ]

    =

    'true'

    ;
  21. $response

    [

    'error'

    ]

    [

    $k

    ]

    [

    'msg'

    ]

    =

    "The entered {$k}

    already exists."

    ;
  22. }

    else

    {
  23. // If data already exists
  24. $response

    [

    'success'

    ]

    [

    $k

    ]

    [

    'status'

    ]

    =

    'true'

    ;
  25. $response

    [

    'success'

    ]

    [

    $k

    ]

    [

    'msg'

    ]

    =

    "The entered {$k}

    is still avalailable."

    ;

  26. }
  27. }
  28. }
  29. return

    $response

    ;
  30. }

  31. if

    (

    isset

    (

    $_POST

    [

    'check_live'

    ]

    )

    &&

    $_POST

    [

    'check_live'

    ]

    ==

    'true'

    )

    {
  32. // If request was executed upon input keyup event
  33. echo

    json_encode

    (

    check_availability(

    )

    )

    ;
  34. $conn

    ->

    close

    (

    )

    ;
  35. }

  36. ?>

Data Insertion API

The following snippet is PHP Script that contains the script for inserting the form's input data values on the database. The script loads the checkAvailability.php file for validating the input value availability before the execution of insertion. Save the file saveEmployee.php.

  1. <?php
  2. require_once

    (

    'db-connect.php'

    )

    ;
  3. require_once

    (

    'checkAvailability.php'

    )

    ;

  4. if

    (

    $_SERVER

    [

    'REQUEST_METHOD'

    ]

    ==

    'POST'

    )

    {
  5. /**
  6. * Check input availability before saving
  7. */
  8. $input_check

    =

    check_availability(

    )

    ;
  9. if

    (

    isset

    (

    $input_check

    [

    'error'

    ]

    )

    )

    {

  10. // If some data already exists
  11. $input_check

    [

    'status'

    ]

    =

    'error'

    ;
  12. $input_check

    [

    'msg'

    ]

    =

    "Some of the input data already exists."

    ;
  13. echo

    json_encode

    (

    $input_check

    )

    ;
  14. exit

    ;

  15. }

  16. // sanitizing input values
  17. foreach

    (

    $_POST

    as

    $k

    =>

    $v

    )

    {
  18. $$k

    =

    addslashes

    (

    $conn

    ->

    real_escape_string

    (

    $v

    )

    )

    ;
  19. }

  20. // Insert SQL Statement
  21. $sql

    =

    "INSERT INTO `employee_list`
  22. (`code`, `name`, `email`, `department`, `designation`)
  23. VALUES
  24. ('{$code}

    ','{$name}

    ','{$email}

    ','{$department}

    ','{$designation}

    ')
  25. "

    ;


  26. // Insert SQL Query
  27. $insert

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  28. if

    (

    $insert

    )

    {
  29. // If insertion is successful
  30. $response

    [

    'status'

    ]

    =

    'success'

    ;
  31. $response

    [

    'msg'

    ]

    =

    'New Employee Data has been added successfully.'

    ;
  32. }

    else

    {
  33. // If insertion has failed
  34. $response

    [

    'status'

    ]

    =

    'success'

    ;
  35. $response

    [

    'msg'

    ]

    =

    'Employee Data has failed to insert. Error: '

    .

    $conn

    ->

    error

    ;
  36. }

  37. echo

    json_encode

    (

    $response

    )

    ;
  38. }

  39. $conn

    ->

    close

    (

    )

    ;

That's it! You can now test the sample web application on your end and see if it achieves the objective of this tutorial. I have provided also the source code zip file on this article. You can download it by clicking the Download Button below this article.

Snapshots

Here are some of the snapshots of the application's output.

Form Interface

Input Data Availability Message (Available)

Input Data Availability Message (Existing)

Form's Interface with Alerts

That's the end of this tutorial. I hope this Live Checking of Input Data Availability using VueJS and PHP Tutorial helps 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 hidden text.
 

452,292

323,341

323,350

Top