• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

AngularJS Inline Edit using PHP/MySQLi

chippak

RAT Controller
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
57
Likes
104
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 900 XP
Getting Started

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

Creating our Database

First, we are going to create our database and insert sample data.

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;

  1. INSERT

    INTO

    `members`

    (

    `memid`

    ,

    `firstname`

    ,

    `lastname`

    ,

    `address`

    )

    VALUES


  2. (

    1

    ,

    'Neovic'

    ,

    'Devierte'

    ,

    'Silay'

    )

    ,
  3. (

    2

    ,

    'Julyn'

    ,

    'Divinagracia'

    ,

    'E.B.'

    )

    ,
  4. (

    3

    ,

    'Gemalyn'

    ,

    'Cepe'

    ,

    'Bohol'

    )

    ,
  5. (

    4

    ,

    'Matet'

    ,

    'Devierte'

    ,

    'Silay City'

    )

    ,
  6. (

    5

    ,

    'Tintin'

    ,

    'Devierte'

    ,

    'Silay City'

    )

    ,
  7. (

    6

    ,

    'Bien'

    ,

    'Devierte'

    ,

    'Cebu City'

    )

    ,
  8. (

    9

    ,

    'Janna '

    ,

    'Atienza'

    ,

    'Talisay City'

    )

    ,
  9. (

    10

    ,

    'Desire'

    ,

    'Osorio'

    ,

    'Bacolod City'

    )

    ;

database_6_61.png

index.html

This is our index which contains our table.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "app"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    AngularJS Inline Edit 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=

    "memberdata"

    ng-init=

    "fetch()"

    >
  10. <div

    class

    =

    "container"

    >
  11. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Inline Edit using PHP/MySQLi</

    h1

    >
  12. <div

    class

    =

    "row"

    >
  13. <div

    class

    =

    "col-md-10 col-md-offset-1"

    >
  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

    >

    {{ message }}
  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

    >

    {{ message }}
  21. </

    div

    >
  22. <table

    class

    =

    "table table-bordered table-striped"

    style

    =

    "margin-top:10px;"

    >
  23. <thead

    >
  24. <tr

    >
  25. <th

    >

    First Name</

    th

    >
  26. <th

    >

    Last Name</

    th

    >
  27. <th

    >

    Address</

    th

    >
  28. <th

    >

    Action</

    th

    >
  29. </

    tr

    >
  30. </

    thead

    >
  31. <tbody

    >
  32. <tr

    ng-repeat=

    "member in members"

    ng-include=

    "getTemplate(member)"

    >
  33. </

    tr

    >
  34. </

    tbody

    >
  35. </

    table

    >
  36. <script

    type

    =

    "text/ng-template"

    id

    =

    "normal"

    >
  37. <td

    >

    {{member.firstname}}</

    td

    >
  38. <td

    >

    {{member.lastname}}</

    td

    >
  39. <td

    >

    {{member.address}}</

    td

    >
  40. <td

    ><button

    type

    =

    "button"

    ng-click=

    "edit(member)"

    class

    =

    "btn btn-success"

    ><span

    class

    =

    "glyphicon glyphicon-pencil"

    ></

    span

    >

    Edit</

    button

    ></

    td

    >
  41. </

    script

    >
  42. <script

    type

    =

    "text/ng-template"

    id

    =

    "edit"

    >
  43. <td

    ><input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "editmember.firstname"

    ></

    td

    >
  44. <td

    ><input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "editmember.lastname"

    ></

    td

    >
  45. <td

    ><input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "editmember.address"

    ></

    td

    >
  46. <td

    >
  47. <button

    type

    =

    "button"

    ng-click=

    "save(editmember)"

    class

    =

    "btn btn-primary"

    ><span

    class

    =

    "glyphicon glyphicon-floppy-disk"

    ></

    span

    >

    Save</

    button

    >
  48. <button

    type

    =

    "button"

    ng-click=

    "reset()"

    class

    =

    "btn btn-default"

    ><span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    >

    Cancel</

    button

    >
  49. </

    td

    >
  50. </

    script

    >
  51. </

    div

    >
  52. </

    div

    >
  53. </

    div

    >
  54. <script

    src

    =

    "angular.js"

    ></

    script

    >
  55. </

    body

    >
  56. </

    html

    >

angular.js

This contains our angular js script.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    ]

    )

    ;
  2. app.controller

    (

    'memberdata'

    ,

    function

    (

    $scope,

    $http)

    {
  3. $scope.success

    =

    false

    ;
  4. $scope.error

    =

    false

    ;
  5. $scope.editMode

    =

    false

    ;

  6. $scope.editmember

    =

    {

    }

    ;

  7. $scope.getTemplate

    =

    function

    (

    member)

    {
  8. if

    (

    member.memid

    ===

    $scope.editmember

    .memid

    )

    return

    'edit'

    ;
  9. else

    return

    'normal'

    ;
  10. }

    ;

  11. $scope.fetch

    =

    function

    (

    )

    {
  12. $http.get

    (

    'fetch.php'

    )

    .success

    (

    function

    (

    data)

    {
  13. $scope.members

    =

    data;
  14. }

    )

    ;
  15. }

  16. $scope.edit

    =

    function

    (

    member)

    {
  17. $scope.editmember

    =

    angular.copy

    (

    member)

    ;
  18. }

  19. $scope.save

    =

    function

    (

    editmember)

    {
  20. $http.post

    (

    'save.php'

    ,

    editmember)
  21. .success

    (

    function

    (

    data)

    {
  22. console.log

    (

    data)

    ;
  23. if

    (

    data.error

    )

    {
  24. $scope.error

    =

    true

    ;
  25. $scope.success

    =

    false

    ;
  26. $scope.message

    =

    data.message

    ;
  27. }
  28. else

    {
  29. $scope.fetch

    (

    )

    ;
  30. $scope.reset

    (

    )

    ;
  31. $scope.success

    =

    true

    ;
  32. $scope.error

    =

    false

    ;
  33. $scope.message

    =

    data.message

    ;
  34. }
  35. }

    )

    ;
  36. }

  37. $scope.reset

    =

    function

    (

    )

    {
  38. $scope.editmember

    =

    {

    }

    ;
  39. }

    ;

  40. $scope.clearMessage

    =

    function

    (

    )

    {
  41. $scope.success

    =

    false

    ;
  42. $scope.error

    =

    false

    ;
  43. }

  44. }

    )

    ;

fetch.php

This is our 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 members"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $output

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $output

    )

    ;
  10. ?>

save.php

Lastly, this is our PHP code/api in saving the changes we made to our table.

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'angular'

    )

    ;

  3. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  4. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  5. $memid

    =

    $data

    ->

    memid

    ;
  6. $firstname

    =

    $data

    ->

    firstname

    ;
  7. $lastname

    =

    $data

    ->

    lastname

    ;
  8. $address

    =

    $data

    ->

    address

    ;

  9. $sql

    =

    "UPDATE members SET firstname = '$firstname

    ', lastname = '$lastname

    ', address = '$address

    ' WHERE memid = '$memid

    '"

    ;
  10. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  11. if

    (

    $query

    )

    {
  12. $out

    [

    'message'

    ]

    =

    "Member updated Successfully"

    ;
  13. }
  14. else

    {
  15. $out

    [

    'error'

    ]

    =

    true

    ;
  16. $out

    [

    'message'

    ]

    =

    "Cannot update Member"

    ;
  17. }

  18. echo

    json_encode

    (

    $out

    )

    ;
  19. ?>

That ends this tutorial. Happy Coding :)


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

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,502

355,786

355,794

Top