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

Create an Alert using Vue.js with PHP

puffy009

Freelance Platform Wizard
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
94
Likes
92
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

I've used CDN of Bootstrap, Vue.js and Axios.js in this tutorial, so you need internet connection for them to work.

Creating our Database

1. Open phpMyAdmin.
2. Click databases, create a database and name it as vuealert.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `members`

    (
  2. `memid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `email`

    VARCHAR

    (

    60

    )

    NOT

    NULL

    ,
  4. `password`

    VARCHAR

    (

    50

    )

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `memid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_32.png

style.css

This is our customize CSS.

  1. .topcorner{
  2. position

    :

    absolute

    ;
  3. top

    :

    5px

    ;
  4. right

    :

    5px

    ;
  5. }
  6. .alert_danger

    {
  7. padding

    :

    15px

    ;
  8. background-color

    :

    #f44336

    ;
  9. color

    :

    white

    ;
  10. }

  11. .alert_success

    {
  12. padding

    :

    15px

    ;
  13. background-color

    :

    #4CAF50

    ;
  14. color

    :

    white

    ;
  15. }

  16. .closebutton

    {
  17. margin-left

    :

    15px

    ;
  18. color

    :

    white

    ;
  19. font-weight

    :

    bold

    ;
  20. float

    :

    right

    ;
  21. font-size

    :

    22px

    ;
  22. line-height

    :

    20px

    ;
  23. cursor

    :

    pointer

    ;
  24. transition

    :

    0.3s

    ;
  25. }

  26. .closebutton

    :

    hover

    {
  27. color

    :

    black

    ;
  28. }

index.php

This contains our form to trigger our alert and also contains our sample table.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    Create an Alert 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. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "style.css"

    >
  9. </

    head

    >
  10. <body

    >
  11. <div

    id

    =

    "alert"

    >
  12. <div

    class

    =

    "topcorner alert_danger"

    v-if=

    "isError"

    >
  13. <span

    class

    =

    "closebutton"

    @click=

    "clearMessage();"

    >

    &times;

    </

    span

    >
  14. <span

    class

    =

    "glyphicon glyphicon-alert"

    ></

    span

    >

    {{ responseMessage }}
  15. </

    div

    >
  16. <div

    class

    =

    "topcorner alert_success"

    v-if=

    "isSuccess"

    >
  17. <span

    class

    =

    "closebutton"

    @click=

    "clearMessage();"

    >

    &times;

    </

    span

    >
  18. <span

    class

    =

    "glyphicon glyphicon-check-square-o"

    ></

    span

    >

    {{ responseMessage }}
  19. </

    div

    >
  20. <div

    class

    =

    "container"

    >
  21. <h1

    class

    =

    "page-header text-center"

    >

    Create an Alert using Vue.js with PHP</

    h1

    >
  22. <div

    class

    =

    "col-md-4"

    >
  23. <div

    class

    =

    "form-group"

    >
  24. <label

    >

    Email:</

    label

    >
  25. <input

    type

    =

    "text"

    class

    =

    "form-control"

    v-model=

    "newMember.email"

    v-on:keyup=

    "keymonitor"

    >
  26. </

    div

    >
  27. <div

    class

    =

    "form-group"

    >
  28. <label

    >

    Password:</

    label

    >
  29. <input

    type

    =

    "text"

    class

    =

    "form-control"

    v-model=

    "newMember.password"

    v-on:keyup=

    "keymonitor"

    >
  30. </

    div

    >
  31. <button

    class

    =

    "btn btn-primary"

    @click=

    "insertMember();"

    ><span

    class

    =

    "glyphicon glyphicon-floppy-disk"

    ></

    span

    >

    Save</

    button

    >

    <button

    class

    =

    "btn btn-danger"

    @click=

    "clearForm();"

    ><span

    class

    =

    "glyphicon glyphicon-refresh"

    ></

    span

    >

    Clear</

    button

    >
  32. </

    div

    >
  33. <div

    class

    =

    "col-md-8"

    >
  34. <table

    class

    =

    "table table-bordered table-striped"

    >
  35. <thead

    >
  36. <th

    >

    Member ID</

    th

    >
  37. <th

    >

    Email</

    th

    >
  38. <th

    >

    Password</

    th

    >
  39. </

    thead

    >
  40. <tbody

    >
  41. <tr

    v-for

    =

    "member in members"

    >
  42. <td

    >

    {{ member.memid }}</

    td

    >
  43. <td

    >

    {{ member.email }}</

    td

    >
  44. <td

    >

    {{ member.password }}</

    td

    >
  45. </

    tr

    >
  46. </

    tbody

    >
  47. </

    table

    >
  48. </

    div

    >
  49. </

    div

    >
  50. </

    div

    >
  51. <script

    src

    =

    "app.js"

    ></

    script

    >
  52. </

    body

    >
  53. </

    html

    >

app.js

This contains our vue.js codes.

  1. var

    app =

    new

    Vue(

    {
  2. el:

    '#alert'

    ,
  3. data:

    {
  4. newMember:

    {

    email:

    ''

    ,

    password:

    ''

    }

    ,
  5. alertMessage:

    false

    ,
  6. isSuccess:

    false

    ,
  7. isError:

    false

    ,
  8. responseMessage:

    ""

    ,
  9. members:

    [

    ]
  10. }

    ,

  11. mounted:

    function

    (

    )

    {
  12. this

    .fetchMembers

    (

    )

    ;
  13. }

    ,

  14. methods:

    {
  15. keymonitor:

    function

    (

    event)

    {
  16. if

    (

    event.key

    ==

    "Enter"

    )

    {
  17. app.insertMember

    (

    )

    ;
  18. }
  19. }

    ,

  20. fetchMembers:

    function

    (

    )

    {
  21. axios.post

    (

    'action.php'

    )
  22. .then

    (

    function

    (

    response)

    {
  23. app.members

    =

    response.data

    .members

    ;
  24. }

    )

    ;
  25. }

    ,

  26. insertMember:

    function

    (

    )

    {
  27. var

    memberForm =

    app.toFormData

    (

    app.newMember

    )

    ;
  28. axios.post

    (

    'action.php?action=add'

    ,

    memberForm)
  29. .then

    (

    function

    (

    response)

    {
  30. console.log

    (

    response)

    ;
  31. if

    (

    response.data

    .error

    )

    {
  32. app.alertMessage

    =

    true

    ;
  33. app.isError

    =

    true

    ;
  34. app.isSuccess

    =

    false

    ;
  35. app.responseMessage

    =

    response.data

    .message

    ;
  36. setTimeout(

    function

    (

    )

    {
  37. app.clearMessage

    (

    )

    ;
  38. }

    ,

    3000

    )

    ;
  39. }
  40. else

    {
  41. app.isSuccess

    =

    true

    ;
  42. app.isError

    =

    false

    ;
  43. app.alertMessage

    =

    true

    ;
  44. app.responseMessage

    =

    response.data

    .message

    ;
  45. app.newMember

    =

    {

    email:

    ''

    ,

    password:

    ''

    }

    ;
  46. app.fetchMembers

    (

    )

    ;
  47. setTimeout(

    function

    (

    )

    {
  48. app.clearMessage

    (

    )

    ;
  49. }

    ,

    3000

    )

    ;
  50. }
  51. }

    )

    ;
  52. }

    ,

  53. toFormData:

    function

    (

    obj)

    {
  54. var

    form_data =

    new

    FormData(

    )

    ;
  55. for

    (

    var

    key in

    obj)

    {
  56. form_data.append

    (

    key,

    obj[

    key]

    )

    ;
  57. }
  58. return

    form_data;
  59. }

    ,

  60. clearMessage:

    function

    (

    )

    {
  61. app.isError

    =

    false

    ;
  62. app.isSuccess

    =

    false

    ;
  63. }

    ,

  64. clearForm:

    function

    (

    )

    {
  65. app.newMember

    =

    app.newMember

    =

    {

    email:

    ''

    ,

    password:

    ''

    }

    ;
  66. }



  67. }
  68. }

    )

    ;

action.php

Lastly, this our PHP code which contains our validations to trigger alerts.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "vuealert"

    )

    ;

  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

    ==

    'add'

    )

    {
  21. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  22. $password

    =

    $_POST

    [

    'password'

    ]

    ;

  23. if

    (

    $email

    ==

    ''

    )

    {
  24. $out

    [

    'error'

    ]

    =

    true

    ;
  25. $out

    [

    'message'

    ]

    =

    'Add Member Failed. Username Empty.'

    ;
  26. }
  27. elseif

    (

    !

    filter_var

    (

    $email

    ,

    FILTER_VALIDATE_EMAIL)

    )

    {
  28. $out

    [

    'error'

    ]

    =

    true

    ;
  29. $out

    [

    'message'

    ]

    =

    'Add Member Failed. Invalid Email Format'

    ;
  30. }
  31. elseif

    (

    $password

    ==

    ''

    )

    {
  32. $out

    [

    'error'

    ]

    =

    true

    ;
  33. $out

    [

    'message'

    ]

    =

    'Add Member Failed. Password Empty.'

    ;
  34. }
  35. elseif

    (

    !

    preg_match

    (

    "/^[a-zA-Z_1-9]*$/"

    ,

    $password

    )

    )

    {
  36. $out

    [

    'error'

    ]

    =

    true

    ;
  37. $out

    [

    'message'

    ]

    =

    'Underscore and Special Characters not allowed in Password'

    ;
  38. }
  39. else

    {
  40. $sql

    =

    "insert into members (email, password) values ('$email

    ', '$password

    ')"

    ;
  41. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  42. if

    (

    $query

    )

    {
  43. $out

    [

    'message'

    ]

    =

    'Member Successfully Added'

    ;
  44. }
  45. else

    {
  46. $out

    [

    'error'

    ]

    =

    true

    ;
  47. $out

    [

    'message'

    ]

    =

    'Error in Adding Occured'

    ;
  48. }

  49. }
  50. }


  51. $conn

    ->

    close

    (

    )

    ;

  52. header

    (

    "Content-type: application/json"

    )

    ;
  53. echo

    json_encode

    (

    $out

    )

    ;
  54. die

    (

    )

    ;

  55. ?>

That ends this tutorial. Happy Coding :)


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

452,292

324,736

324,744

Top