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

AngularJS Adding Form Fields Dynamically with PHP/MySQLi

orum4479

Anime Culture Researcher
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
156
Likes
187
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

I've used CDN for Bootstrap and Angular JS in this tutorial so you need internet connection for them to work.

Creating our Database

First, we are going to create our database.

1. Open phpMyAdmin.
2. Click databases, create a database and name it as angular.
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. `firstname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. `lastname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  5. `address`

    text

    NOT

    NULL

    ,
  6. PRIMARY KEY

    (

    `memid`

    )
  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

database_6_50.png

index.html

Next, this is our index that contains out add form.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "app"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    AngularJS Adding Form Fields Dynamically with PHP/MySQLi</

    title

    >
  6. <link

    href

    =

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

    rel

    =

    "stylesheet"

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  8. </

    head

    >
  9. <body

    ng-controller=

    "memberdata"

    ng-init=

    "fetch()"

    >
  10. <div

    class

    =

    "container"

    >
  11. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Adding Form Fields Dynamically with PHP/MySQLi</

    h1

    >
  12. <div

    class

    =

    "row"

    >
  13. <div

    class

    =

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

    >
  14. <div

    class

    =

    "alert alert-success text-center"

    ng-show=

    "success"

    >
  15. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clearMessage()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  16. <i

    class

    =

    "fa fa-check"

    ></

    i

    >

    {{ successMessage }}
  17. </

    div

    >
  18. <div

    class

    =

    "alert alert-danger text-center"

    ng-show=

    "error"

    >
  19. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clearMessage()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  20. <i

    class

    =

    "fa fa-warning"

    ></

    i

    >

    {{ errorMessage }}
  21. </

    div

    >
  22. <form

    name

    =

    "addForm"

    novalidate>
  23. <fieldset

    ng-repeat=

    "memberfield in memberfields"

    >
  24. <div

    class

    =

    "panel panel-default"

    >
  25. <div

    class

    =

    "panel-body"

    >
  26. <div

    class

    =

    "row"

    >
  27. <div

    class

    =

    "col-md-3"

    >
  28. <input

    type

    =

    "text"

    placeholder=

    "Firstname"

    class

    =

    "form-control"

    ng-model=

    "memberfield.firstname"

    required>
  29. </

    div

    >
  30. <div

    class

    =

    "col-md-3"

    >
  31. <input

    type

    =

    "text"

    placeholder=

    "Lastname"

    class

    =

    "form-control"

    ng-model=

    "memberfield.lastname"

    required>
  32. </

    div

    >
  33. <div

    class

    =

    "col-md-5"

    >
  34. <input

    type

    =

    "text"

    placeholder=

    "Address"

    class

    =

    "form-control"

    ng-model=

    "memberfield.address"

    required>
  35. </

    div

    >
  36. <button

    class

    =

    "btn btn-danger btn-sm"

    ng-show=

    "$last"

    ng-click=

    "removeField()"

    ><span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    ></

    button

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    div

    >
  40. </

    fieldset

    >
  41. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-click=

    "newfield()"

    ><span

    class

    =

    "glyphicon glyphicon-plus"

    ></

    span

    >

    Add</

    button

    >
  42. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-disabled

    =

    "addForm.$invalid"

    ng-click=

    "submitForm()"

    ><span

    class

    =

    "glyphicon glyphicon-floppy-disk"

    ></

    span

    >

    Save</

    button

    >
  43. </

    form

    >
  44. <table

    class

    =

    "table table-bordered table-striped"

    style

    =

    "margin-top:10px;"

    >
  45. <thead

    >
  46. <tr

    >
  47. <th

    >

    First Name</

    th

    >
  48. <th

    >

    Last Name</

    th

    >
  49. <th

    >

    Address</

    th

    >
  50. </

    tr

    >
  51. </

    thead

    >
  52. <tbody

    >
  53. <tr

    ng-repeat=

    "member in members"

    >
  54. <td

    >

    {{ member.firstname }}</

    td

    >
  55. <td

    >

    {{ member.lastname }}</

    td

    >
  56. <td

    >

    {{ member.address }}</

    td

    >
  57. </

    tr

    >
  58. </

    tbody

    >
  59. </

    table

    >
  60. </

    div

    >
  61. </

    div

    >
  62. </

    div

    >
  63. <script

    src

    =

    "angular.js"

    ></

    script

    >
  64. </

    body

    >
  65. </

    html

    >

angular.js

This contains our angular js scripts.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    ]

    )

    ;
  2. app.controller

    (

    'memberdata'

    ,

    function

    (

    $scope,

    $http)

    {
  3. $scope.success

    =

    false

    ;
  4. $scope.error

    =

    false

    ;

  5. $scope.fetch

    =

    function

    (

    )

    {
  6. $http.get

    (

    "fetch.php"

    )

    .success

    (

    function

    (

    data)

    {
  7. $scope.members

    =

    data;
  8. }

    )

    ;
  9. }

  10. $scope.memberfields

    =

    [

    {

    id:

    'field1'

    }

    ]

    ;

  11. $scope.newfield

    =

    function

    (

    )

    {
  12. var

    newItem =

    $scope.memberfields

    .length

    +

    1

    ;
  13. $scope.memberfields

    .push

    (

    {

    'id'

    :

    'field'

    +

    newItem}

    )

    ;
  14. }

  15. $scope.submitForm

    =

    function

    (

    )

    {
  16. $http.post

    (

    'add.php'

    ,

    $scope.memberfields

    )
  17. .success

    (

    function

    (

    data)

    {
  18. if

    (

    data.error

    )

    {
  19. $scope.error

    =

    true

    ;
  20. $scope.success

    =

    false

    ;
  21. $scope.errorMessage

    =

    data.message

    ;
  22. }
  23. else

    {
  24. $scope.error

    =

    false

    ;
  25. $scope.success

    =

    true

    ;
  26. $scope.successMessage

    =

    data.message

    ;
  27. $scope.fetch

    (

    )

    ;
  28. $scope.memberfields

    =

    [

    {

    id:

    'field1'

    }

    ]

    ;
  29. }
  30. }

    )

    ;
  31. }

  32. $scope.removeField

    =

    function

    (

    )

    {
  33. var

    lastItem =

    $scope.memberfields

    .length

    -

    1

    ;
  34. $scope.memberfields

    .splice

    (

    lastItem)

    ;
  35. }

    ;

  36. $scope.clearMessage

    =

    function

    (

    )

    {
  37. $scope.success

    =

    false

    ;
  38. $scope.error

    =

    false

    ;
  39. }
  40. }

    )

    ;

fetch.php

This is our PHP api/code that fetches data from our MySQL database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $output

    =

    array

    (

    )

    ;
  4. $sql

    =

    "SELECT * FROM members"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $output

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $output

    )

    ;
  10. ?>

add.php

Lastly, this is our PHP api/code in adding one or multiple rows into our database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $count

    =

    count

    (

    $data

    )

    ;

  5. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  6. foreach

    (

    $data

    as

    $key

    =>

    $value

    )

    {
  7. $firstname

    =

    $value

    ->

    firstname

    ;
  8. $lastname

    =

    $value

    ->

    lastname

    ;
  9. $address

    =

    $value

    ->

    address

    ;

  10. $sql

    =

    "INSERT INTO members (firstname, lastname, address) VALUES ('$firstname

    ', '$lastname

    ', '$address

    ')"

    ;
  11. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  12. }

  13. if

    (

    $query

    )

    {
  14. $out

    [

    'message'

    ]

    =

    "($count

    ) Member/s added successfully"

    ;
  15. }
  16. else

    {
  17. $out

    [

    'error'

    ]

    =

    true

    ;
  18. $out

    [

    'message'

    ]

    =

    "Cannot add Member(s)"

    ;
  19. }

  20. echo

    json_encode

    (

    $out

    )

    ;
  21. ?>

That ends this tutorial. Happy Coding :)


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

452,292

323,517

323,526

Top