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

AngularJS Select - Dynamically add Options using PHP/MySQLi

purplevelvet

Cyber Footprint Reduction Specialist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
169
Likes
152
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 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're gonna 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

    `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;

index.html

This is our index that contains our add form, table and select input.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "app"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    AngularJS Select - Dynamically add Options 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. <style

    type

    =

    "text/css"

    >
  9. .errortext{
  10. color:red;
  11. }
  12. </

    style

    >
  13. </

    head

    >
  14. <body

    ng-controller=

    "myController"

    ng-init=

    "fetch()"

    >
  15. <div

    class

    =

    "container"

    >
  16. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Select - Dynamically add Options using PHP/MySQLi</

    h1

    >
  17. <div

    class

    =

    "col-md-2 col-md-offset-1"

    >
  18. <h3

    >

    Add new Fruit</

    h3

    >
  19. <input

    type

    =

    "text"

    placeholder=

    "Fruit Name"

    class

    =

    "form-control"

    ng-model=

    "newFruit.fruitname"

    ><br

    >
  20. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-click=

    "add()"

    ><span

    class

    =

    "glyphicon glyphicon-floppy-disk"

    ></

    span

    >

    Save</

    button

    >
  21. </

    div

    >
  22. <div

    class

    =

    "col-md-6"

    >
  23. <h3

    >

    Fruit Table</

    h3

    >
  24. <table

    class

    =

    "table table-bordered table-striped"

    >
  25. <thead

    >
  26. <tr

    >
  27. <th

    >

    Fruit ID</

    th

    >
  28. <th

    >

    Fruit Name</

    th

    >
  29. <tr

    >
  30. </

    thead

    >
  31. <tbody

    >
  32. <tr

    ng-repeat=

    "fruit in fruits"

    >
  33. <td

    >

    {{ fruit.fruitid }}</

    td

    >
  34. <td

    >

    {{ fruit.fruitname }}</

    td

    >
  35. </

    tr

    >
  36. </

    tbody

    >
  37. </

    table

    >
  38. </

    div

    >
  39. <div

    class

    =

    "col-md-2"

    >
  40. <h3

    >

    Select Fruit</

    h3

    >
  41. <!-- <select ng-model="selectedFruit">
  42. <option ng-repeat="x in fruits" value="{{x.fruitid}}">{{x.fruitname}}</option>
  43. </select> -->
  44. <select

    ng-model=

    "selectedFruit"

    ng-options=

    "x.fruitname for x in fruits"

    class

    =

    "form-control"

    >
  45. </

    select

    >
  46. <hr

    >
  47. <p

    ><b

    >

    You selected:</

    b

    >

    {{selectedFruit.fruitname}}</

    p

    >
  48. <p

    ><b

    >

    ID:</

    b

    >

    {{selectedFruit.fruitid}}</

    p

    >
  49. <!-- <p><b>You selected:</b> {{selectedFruit}}</p> -->
  50. </

    div

    >
  51. </

    div

    >
  52. <script

    src

    =

    "angular.js"

    ></

    script

    >
  53. </

    body

    >
  54. </

    html

    >

angular.js

This contains our angular js scripts.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    ]

    )

    ;
  2. app.controller

    (

    'myController'

    ,

    function

    (

    $scope,

    $http)

    {
  3. $scope.fetch

    =

    function

    (

    )

    {
  4. $http.get

    (

    "fetch.php"

    )

    .success

    (

    function

    (

    data)

    {
  5. $scope.fruits

    =

    data;
  6. }

    )

    ;
  7. }

  8. $scope.add

    =

    function

    (

    )

    {
  9. //console.log($scope.newFruit);
  10. $http.post

    (

    "add.php"

    ,

    $scope.newFruit

    )
  11. .success

    (

    function

    (

    )

    {
  12. $scope.newFruit

    =

    ''

    ;
  13. $scope.fetch

    (

    )

    ;
  14. }

    )

    ;
  15. }
  16. }

    )

    ;

fetch.php

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

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $output

    =

    array

    (

    )

    ;
  4. $sql

    =

    "SELECT * FROM fruits"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $output

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $output

    )

    ;
  10. ?>

add.php

This is our PHP code/api in adding data into our table.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $fruitname

    =

    $data

    ->

    fruitname

    ;

  5. $sql

    =

    "INSERT INTO fruits (fruitname) VALUES ('$fruitname

    ')"

    ;
  6. $conn

    ->

    query

    (

    $sql

    )

    ;

  7. ?>

ng-options vs ng-repeat

In this tutorial we have used ng-options. By doing so, the options in our select input are objects. Whereas, when using ng-repeat, the options become a string.

I've added in the comments in index.html the code in using ng-repeat for input select. You can exchange between ng-options for you to spot the difference.

That ends this tutorial. Happy Coding :)


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

452,496

333,651

333,659

Top