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

AngularJS Save Selected Options (ng-option) using PHP/MySQLi

krassablu

Rookie
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
143
Likes
87
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.

In the previous tutorial, we have tackled on how to Dynamically Add Options. In this tutorial, we are going to save this selected option.

Creating our Database

First, we're gonna create our database and insert data for our options.

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

    `fruits`

    (
  2. `fruitid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `fruitname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. PRIMARY KEY

    (

    `fruitid`

    )
  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  6. CREATE

    TABLE

    `vegetables`

    (
  7. `vegetableid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  8. `vegetablename`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  9. PRIMARY KEY

    (

    `vegetableid`

    )
  10. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  11. CREATE

    TABLE

    `purchases`

    (
  12. `purchaseid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  13. `vegetableid`

    int

    (

    11

    )

    NOT

    NULL

    ,
  14. `fruitid`

    int

    (

    11

    )

    NOT

    NULL

    ,
  15. PRIMARY KEY

    (

    `purchaseid`

    )
  16. `date_

    purchase`

    datetime

    NOT

    NULL


  17. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  1. INSERT

    INTO

    `fruits`

    (

    `fruitid`

    ,

    `fruitname`

    )

    VALUES


  2. (

    1

    ,

    'Apple'

    )

    ,
  3. (

    2

    ,

    'Orange'

    )

    ,
  4. (

    3

    ,

    'Strawberry'

    )

    ,
  5. (

    4

    ,

    'Pineapple'

    )

    ,
  6. (

    5

    ,

    'Star Apple'

    )

    ,
  7. (

    7

    ,

    'Banana'

    )

    ,
  8. (

    8

    ,

    'Lemon'

    )

    ,
  9. (

    9

    ,

    'Mango'

    )

    ,
  10. (

    10

    ,

    'Guava'

    )

    ,
  11. (

    11

    ,

    'Watermelon'

    )

    ,
  12. (

    12

    ,

    'Avocado'

    )

    ,
  13. (

    13

    ,

    'Apricot'

    )

    ,
  14. (

    14

    ,

    'Blackberry'

    )

    ,
  15. (

    15

    ,

    'Coconut'

    )

    ,
  16. (

    16

    ,

    'Melon'

    )

    ,
  17. (

    17

    ,

    'Papaya'

    )

    ,
  18. (

    18

    ,

    'Peach'

    )

    ,
  19. (

    19

    ,

    'Pomelo'

    )

    ,
  20. (

    20

    ,

    'Grapes'

    )

    ;

  21. INSERT

    INTO

    `purchases`

    (

    `purchaseid`

    ,

    `vegetableid`

    ,

    `fruitid`

    ,

    `date_

    purchase`

    )

    VALUES


  22. (

    1

    ,

    9

    ,

    4

    ,

    '2018-01-11 14:21:16'

    )

    ,
  23. (

    2

    ,

    8

    ,

    10

    ,

    '2018-01-11 14:42:57'

    )

    ,
  24. (

    3

    ,

    2

    ,

    7

    ,

    '2018-01-11 15:22:34'

    )

    ,
  25. (

    4

    ,

    6

    ,

    3

    ,

    '2018-01-11 15:27:44'

    )

    ,
  26. (

    5

    ,

    10

    ,

    5

    ,

    '2018-01-11 15:28:29'

    )

    ,
  27. (

    6

    ,

    4

    ,

    17

    ,

    '2018-01-11 15:30:57'

    )

    ,
  28. (

    7

    ,

    9

    ,

    5

    ,

    '2018-01-11 15:37:16'

    )

    ,
  29. (

    8

    ,

    4

    ,

    8

    ,

    '2018-01-11 15:39:10'

    )

    ;

database_6_52.png

index.html

This is our index which contains our form and table.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "app"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    AngularJS Save Selected Options (ng-option) using 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=

    "myController"

    >
  10. <div

    class

    =

    "container"

    >
  11. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Save Selected Options (ng-option) using PHP/MySQLi</

    h1

    >
  12. <div

    class

    =

    "row"

    >
  13. <div

    class

    =

    "col-md-3 col-md-offset-1"

    ng-init=

    "fetch()"

    >
  14. <h3

    >

    Select Fruit</

    h3

    >
  15. <form

    name

    =

    "purchaseForm"

    novalidate>
  16. <select

    ng-model=

    "selectedFruit"

    ng-options=

    "f.fruitname for f in fruits"

    class

    =

    "form-control"

    placeholder=

    "Select Fruit"

    required>
  17. <option

    value

    =

    ""

    >

    -- Select Fruit --</

    option

    >
  18. </

    select

    >
  19. <p

    style

    =

    "margin-top:10px;"

    ><b

    >

    Fruit selected:</

    b

    >

    {{selectedFruit.fruitname}}</

    p

    >
  20. <hr

    >
  21. <h3

    >

    Select Vegetable</

    h3

    >
  22. <select

    ng-model=

    "selectedVegetable"

    ng-options=

    "v.vegetablename for v in vegetables"

    class

    =

    "form-control"

    required>
  23. <option

    value

    =

    ""

    >

    -- Select Vegetable --</

    option

    >
  24. </

    select

    >
  25. <p

    style

    =

    "margin-top:10px;"

    ><b

    >

    Vegetable selected:</

    b

    >

    {{selectedVegetable.vegetablename}}</

    p

    >
  26. <hr

    >
  27. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-click=

    "purchase()"

    ng-disabled

    =

    "purchaseForm.$invalid"

    >

    Purchase</

    button

    >
  28. </

    form

    >
  29. <div

    class

    =

    "alert alert-success text-center"

    ng-show=

    "success"

    style

    =

    "margin-top: 20px"

    >
  30. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clearMsg()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  31. {{ message }}
  32. </

    div

    >
  33. <div

    class

    =

    "alert alert-danger text-center"

    ng-show=

    "error"

    style

    =

    "margin-top: 20px"

    >
  34. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "clearMsg()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  35. {{ message }}
  36. </

    div

    >
  37. </

    div

    >
  38. <div

    class

    =

    "col-md-7"

    ng-init=

    "fetchpurchase()"

    >
  39. <h3

    >

    Purchase Table</

    h3

    >
  40. <table

    class

    =

    "table table-bordered table-striped"

    >
  41. <thead

    >
  42. <tr

    >
  43. <th

    >

    Purchase Date</

    th

    >
  44. <th

    >

    Fruit</

    th

    >
  45. <th

    >

    Vegetable</

    th

    >
  46. <tr

    >
  47. </

    thead

    >
  48. <tbody

    >
  49. <tr

    ng-repeat=

    "purchase in purchases"

    >
  50. <td

    >

    {{ purchase.date_purchase | dateToISO | date:'MMMM dd, yyyy - hh:mm a' }}</

    td

    >
  51. <td

    >

    {{ purchase.fruitname }}</

    td

    >
  52. <td

    >

    {{ purchase.vegetablename }}</

    td

    >
  53. </

    tr

    >
  54. </

    tbody

    >
  55. </

    table

    >
  56. </

    div

    >
  57. </

    div

    >
  58. </

    div

    >
  59. <script

    src

    =

    "angular.js"

    ></

    script

    >
  60. </

    body

    >
  61. </

    html

    >

angular.js

This contains our angular.js scripts.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    ]

    )

    ;
  2. app.controller

    (

    'myController'

    ,

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

    =

    data.fruits

    ;
  8. $scope.vegetables

    =

    data.vegetables

    ;
  9. }

    )

    ;
  10. }

  11. $scope.fetchpurchase

    =

    function

    (

    )

    {
  12. $http.get

    (

    'fetchpurchase.php'

    )

    .success

    (

    function

    (

    data)

    {
  13. $scope.purchases

    =

    data;
  14. }

    )

    ;
  15. }

  16. $scope.purchase

    =

    function

    (

    )

    {
  17. $scope.newPurchase

    =

    [

    ]

    ;

  18. //inserting our selected object
  19. $scope.newPurchase

    .push

    (

    $scope.selectedVegetable

    )

    ;
  20. $scope.newPurchase

    .push

    (

    $scope.selectedFruit

    )

    ;

  21. $http.post

    (

    'purchase.php'

    ,

    $scope.newPurchase

    )
  22. .success

    (

    function

    (

    data)

    {
  23. if

    (

    data.error

    )

    {
  24. $scope.error

    =

    true

    ;
  25. $scope.success

    =

    false

    ;
  26. $scope.message

    =

    data.message

    ;
  27. }
  28. else

    {
  29. $scope.success

    =

    true

    ;
  30. $scope.error

    =

    false

    ;
  31. $scope.message

    =

    data.message

    ;
  32. $scope.fetchpurchase

    (

    )

    ;
  33. }
  34. console.log

    (

    data)

    ;
  35. }

    )

    ;
  36. }

  37. $scope.clearMsg

    =

    function

    (

    )

    {
  38. $scope.success

    =

    false

    ;
  39. $scope.error

    =

    false

    ;
  40. }
  41. }

    )

    ;

  42. //convert mysql data to angular date format
  43. app.filter

    (

    'dateToISO'

    ,

    function

    (

    )

    {
  44. return

    function

    (

    input)

    {
  45. input =

    new

    Date

    (

    input)

    .toISOString

    (

    )

    ;
  46. return

    input;
  47. }

    ;
  48. }

    )

    ;

fetch.php

Our PHP api that fetches data from our MySQL database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $output

    =

    array

    (

    )

    ;

  4. //for fruits
  5. $sql

    =

    "SELECT * FROM fruits"

    ;
  6. $fquery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  7. while

    (

    $frow

    =

    $fquery

    ->

    fetch_array

    (

    )

    )

    {
  8. $output

    [

    'fruits'

    ]

    [

    ]

    =

    $frow

    ;
  9. }

  10. //for vegetables
  11. $sql

    =

    "SELECT * FROM vegetables"

    ;
  12. $vquery

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  13. while

    (

    $vrow

    =

    $vquery

    ->

    fetch_array

    (

    )

    )

    {
  14. $output

    [

    'vegetables'

    ]

    [

    ]

    =

    $vrow

    ;
  15. }

  16. echo

    json_encode

    (

    $output

    )

    ;
  17. ?>

purchase.php

This is our PHP api in adding the selected options into our MySQL Database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  5. //getting vegetableid
  6. //note that the first object our $scope.newPurchase is vegetable
  7. $vegetable

    =

    $data

    [

    0

    ]

    ;
  8. $vid

    =

    $vegetable

    ->

    vegetableid

    ;

  9. //getting fruitid
  10. $fruit

    =

    $data

    [

    1

    ]

    ;
  11. $fid

    =

    $fruit

    ->

    fruitid

    ;

  12. $sql

    =

    "INSERT INTO purchases (vegetableid, fruitid, date_purchase) VALUES ('$vid

    ', '$fid

    ', NOW())"

    ;
  13. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  14. if

    (

    $query

    )

    {
  15. $out

    [

    'message'

    ]

    =

    "Purchase added Successfully"

    ;
  16. }
  17. else

    {
  18. $out

    [

    'error'

    ]

    =

    true

    ;
  19. $out

    [

    'messge'

    ]

    =

    "Cannot add Purchase"

    ;
  20. }

  21. echo

    json_encode

    (

    $out

    )

    ;

  22. ?>

fetchpurchase.php

Lastly, this is our another PHP api that fetches data from our MySQL Database.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $output

    =

    array

    (

    )

    ;

  4. $sql

    =

    "SELECT * FROM purchases LEFT JOIN vegetables ON vegetables.vegetableid=purchases.vegetableid LEFT JOIN fruits ON fruits.fruitid=purchases.fruitid ORDER BY date_purchase DESC"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $output

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $output

    )

    ;

  10. ?>

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,360

324,368

Top