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

Vue.js User Registration/Sign up with PHP/MySQLi

Mr-Creampie

Fan Subber
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
172
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
Getting Started

I've use Bootstrap CDN in this tutorial, so you need internet connection for it to work.

Creating our Database

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

  1. CREATE

    TABLE

    `users`

    (
  2. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `email`

    VARCHAR

    (

    150

    )

    NOT

    NULL

    ,
  4. `password`

    VARCHAR

    (

    50

    )

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `userid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_31.png

index.php

This is our index which contains our registration form and also our users table for you to check if registration is successful.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    Vue.js Registration with PHP/MySQLi</

    title

    >
  5. <link

    rel

    =

    "stylesheet"

    href

    =

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

    >
  6. </

    head

    >
  7. <body

    >
  8. <div

    class

    =

    "container"

    >
  9. <h1

    class

    =

    "page-header text-center"

    >

    Vue.js Registration with PHP/MySQLi</

    h1

    >
  10. <div

    id

    =

    "register"

    >
  11. <div

    class

    =

    "col-md-4"

    >

  12. <div

    class

    =

    "panel panel-primary"

    >
  13. <div

    class

    =

    "panel-heading"

    ><span

    class

    =

    "glyphicon glyphicon-user"

    ></

    span

    >

    User Registration</

    div

    >
  14. <div

    class

    =

    "panel-body"

    >
  15. <label

    >

    Email:</

    label

    >
  16. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ref=

    "email"

    v-model=

    "regDetails.email"

    v-on:keyup=

    "keymonitor"

    >
  17. <div

    class

    =

    "text-center"

    v-if=

    "errorEmail"

    >
  18. <span

    style

    =

    "font-size:13px;"

    >

    {{ errorEmail }}</

    span

    >
  19. </

    div

    >
  20. <label

    >

    Password:</

    label

    >
  21. <input

    type

    =

    "password"

    class

    =

    "form-control"

    ref=

    "password"

    v-model=

    "regDetails.password"

    v-on:keyup=

    "keymonitor"

    >
  22. <div

    class

    =

    "text-center"

    v-if=

    "errorPassword"

    >
  23. <span

    style

    =

    "font-size:13px;"

    >

    {{ errorPassword }}</

    span

    >
  24. </

    div

    >
  25. </

    div

    >
  26. <div

    class

    =

    "panel-footer"

    >
  27. <button

    class

    =

    "btn btn-primary btn-block"

    @click=

    "userReg();"

    ><span

    class

    =

    "glyphicon glyphicon-check"

    ></

    span

    >

    Sign up</

    button

    >
  28. </

    div

    >
  29. </

    div

    >

  30. <div

    class

    =

    "alert alert-danger text-center"

    v-if=

    "errorMessage"

    >
  31. <button

    type

    =

    "button"

    class

    =

    "close"

    @click=

    "clearMessage();"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  32. <span

    class

    =

    "glyphicon glyphicon-alert"

    ></

    span

    >

    {{ errorMessage }}
  33. </

    div

    >

  34. <div

    class

    =

    "alert alert-success text-center"

    v-if=

    "successMessage"

    >
  35. <button

    type

    =

    "button"

    class

    =

    "close"

    @click=

    "clearMessage();"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  36. <span

    class

    =

    "glyphicon glyphicon-check"

    ></

    span

    >

    {{ successMessage }}
  37. </

    div

    >

  38. </

    div

    >

  39. <div

    class

    =

    "col-md-8"

    >
  40. <h3

    >

    Users Table</

    h3

    >
  41. <table

    class

    =

    "table table-bordered table-striped"

    >
  42. <thead

    >
  43. <th

    >

    User ID</

    th

    >
  44. <th

    >

    Email</

    th

    >
  45. <th

    >

    Password</

    th

    >
  46. </

    thead

    >
  47. <tbody

    >
  48. <tr

    v-for

    =

    "user in users"

    >
  49. <td

    >

    {{ user.userid }}</

    td

    >
  50. <td

    >

    {{ user.email }}</

    td

    >
  51. <td

    >

    {{ user.password }}</

    td

    >
  52. </

    tr

    >
  53. </

    tbody

    >
  54. </

    table

    >
  55. </

    div

    >
  56. </

    div

    >
  57. </

    div

    >
  58. <script

    src

    =

    "vue.js"

    ></

    script

    >
  59. <script

    src

    =

    "axios.js"

    ></

    script

    >
  60. <script

    src

    =

    "app.js"

    ></

    script

    >
  61. </

    body

    >
  62. </

    html

    >

app.js

  1. var

    app =

    new

    Vue(

    {
  2. el:

    '#register'

    ,
  3. data:

    {
  4. successMessage:

    ""

    ,
  5. errorMessage:

    ""

    ,
  6. errorEmail:

    ""

    ,
  7. errorPassword:

    ""

    ,
  8. users:

    [

    ]

    ,
  9. regDetails:

    {

    email:

    ''

    ,

    password:

    ''

    }

    ,
  10. }

    ,

  11. mounted:

    function

    (

    )

    {
  12. this

    .getAllUsers

    (

    )

    ;
  13. }

    ,

  14. methods:

    {
  15. getAllUsers:

    function

    (

    )

    {
  16. axios.get

    (

    'api.php'

    )
  17. .then

    (

    function

    (

    response)

    {
  18. if

    (

    response.data

    .error

    )

    {
  19. app.errorMessage

    =

    response.data

    .message

    ;
  20. }
  21. else

    {
  22. app.users

    =

    response.data

    .users

    ;
  23. }
  24. }

    )

    ;
  25. }

    ,

  26. userReg:

    function

    (

    )

    {
  27. var

    regForm =

    app.toFormData

    (

    app.regDetails

    )

    ;
  28. axios.post

    (

    'api.php?action=register'

    ,

    regForm)
  29. .then

    (

    function

    (

    response)

    {
  30. console.log

    (

    response)

    ;
  31. if

    (

    response.data

    .email

    )

    {
  32. app.errorEmail

    =

    response.data

    .message

    ;
  33. app.focusEmail

    (

    )

    ;
  34. app.clearMessage

    (

    )

    ;
  35. }
  36. else

    if

    (

    response.data

    .password

    )

    {
  37. app.errorPassword

    =

    response.data

    .message

    ;
  38. app.errorEmail

    =

    ''

    ;
  39. app.focusPassword

    (

    )

    ;
  40. app.clearMessage

    (

    )

    ;
  41. }
  42. else

    if

    (

    response.data

    .error

    )

    {
  43. app.errorMessage

    =

    response.data

    .message

    ;
  44. app.errorEmail

    =

    ''

    ;
  45. app.errorPassword

    =

    ''

    ;
  46. }
  47. else

    {
  48. app.successMessage

    =

    response.data

    .message

    ;
  49. app.regDetails

    =

    {

    email:

    ''

    ,

    password:

    ''

    }

    ;
  50. app.errorEmail

    =

    ''

    ;
  51. app.errorPassword

    =

    ''

    ;
  52. app.getAllUsers

    (

    )

    ;
  53. }
  54. }

    )

    ;
  55. }

    ,

  56. focusEmail:

    function

    (

    )

    {
  57. this

    .$refs.email

    .focus

    (

    )

    ;
  58. }

    ,

  59. focusPassword:

    function

    (

    )

    {
  60. this

    .$refs.password

    .focus

    (

    )

    ;
  61. }

    ,

  62. keymonitor:

    function

    (

    event)

    {
  63. if

    (

    event.key

    ==

    "Enter"

    )

    {
  64. app.userReg

    (

    )

    ;
  65. }
  66. }

    ,

  67. toFormData:

    function

    (

    obj)

    {
  68. var

    form_data =

    new

    FormData(

    )

    ;
  69. for

    (

    var

    key in

    obj)

    {
  70. form_data.append

    (

    key,

    obj[

    key]

    )

    ;
  71. }
  72. return

    form_data;
  73. }

    ,

  74. clearMessage:

    function

    (

    )

    {
  75. app.errorMessage

    =

    ''

    ;
  76. app.successMessage

    =

    ''

    ;
  77. }

  78. }
  79. }

    )

    ;

api.php

This is our PHP API that contains our registration code.

  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

    ,

    'email'

    =>

    false

    ,

    'password'

    =>

    false

    )

    ;

  7. $action

    =

    'read'

    ;

  8. if

    (

    isset

    (

    $_GET

    [

    'action'

    ]

    )

    )

    {
  9. $action

    =

    $_GET

    [

    'action'

    ]

    ;
  10. }


  11. if

    (

    $action

    ==

    'read'

    )

    {
  12. $sql

    =

    "select * from users"

    ;
  13. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  14. $users

    =

    array

    (

    )

    ;

  15. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  16. array_push

    (

    $users

    ,

    $row

    )

    ;
  17. }

  18. $out

    [

    'users'

    ]

    =

    $users

    ;
  19. }

  20. if

    (

    $action

    ==

    'register'

    )

    {

  21. function

    check_input(

    $data

    )

    {
  22. $data

    =

    trim

    (

    $data

    )

    ;
  23. $data

    =

    stripslashes

    (

    $data

    )

    ;
  24. $data

    =

    htmlspecialchars

    (

    $data

    )

    ;
  25. return

    $data

    ;
  26. }

  27. $email

    =

    check_input(

    $_POST

    [

    'email'

    ]

    )

    ;
  28. $password

    =

    check_input(

    $_POST

    [

    'password'

    ]

    )

    ;

  29. if

    (

    $email

    ==

    ''

    )

    {
  30. $out

    [

    'email'

    ]

    =

    true

    ;
  31. $out

    [

    'message'

    ]

    =

    "Email is required"

    ;
  32. }

  33. elseif

    (

    !

    filter_var

    (

    $email

    ,

    FILTER_VALIDATE_EMAIL)

    )

    {
  34. $out

    [

    'email'

    ]

    =

    true

    ;
  35. $out

    [

    'message'

    ]

    =

    "Invalid Email Format"

    ;
  36. }

  37. elseif

    (

    $password

    ==

    ''

    )

    {
  38. $out

    [

    'password'

    ]

    =

    true

    ;
  39. $out

    [

    'message'

    ]

    =

    "Password is required"

    ;
  40. }

  41. else

    {
  42. $sql

    =

    "select * from users where email='$email

    '"

    ;
  43. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  44. if

    (

    $query

    ->

    num_rows

    >

    0

    )

    {
  45. $out

    [

    'email'

    ]

    =

    true

    ;
  46. $out

    [

    'message'

    ]

    =

    "Email already exist"

    ;
  47. }

  48. else

    {
  49. $sql

    =

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

    ', '$password

    ')"

    ;
  50. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  51. if

    (

    $query

    )

    {
  52. $out

    [

    'message'

    ]

    =

    "User Added Successfully"

    ;
  53. }
  54. else

    {
  55. $out

    [

    'error'

    ]

    =

    true

    ;
  56. $out

    [

    'message'

    ]

    =

    "Could not add User"

    ;
  57. }
  58. }
  59. }
  60. }

  61. $conn

    ->

    close

    (

    )

    ;

  62. header

    (

    "Content-type: application/json"

    )

    ;
  63. echo

    json_encode

    (

    $out

    )

    ;
  64. die

    (

    )

    ;


  65. ?>

That ends this tutorial. Happy Coding :)


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

452,469

326,432

326,440

Top