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

CRUD Operation in XML File using PHP with Modal Tutorial

LoCosLoCos77

Witty Commenter
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
52
Likes
122
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
In the previous tutorials with regard to XML, we have discussed on How to Create XML file from MySQL Database using PHP/MySQLi and on How to Display XML File using PHP. This time, we are going to discussed on how to create CRUD Operation on XML File.

This tutorial tackles how to create a CRUD(create, read, update and delete) operation also knows as add, edit, delete in XML Files using PHP with Bootstrap Modal to handle our Forms. XML stands for eXtensible Markup Language which is designed to store and transport data and sometimes used as a database substitute.

Getting Started

I've used bootstrap and jQuery in this tutorial to enable bootstrap modals. These files are included in the downloadable of this tutorial but, if you want, you may download them using the links below:


Preparing our XML File to use

I've included a sample XML

file in the downloadable of this tutorial located in files

folder. This file is what we are going to use in this tutorial. If you want to start from scratch, create a new file inside the files folder naming members.xml

. Copy/Paste

the xml script below in the xml file you created.

  1. <?xml version

    =

    "1.0"

    ?>
  2. <users>
  3. </

    users>

Displaying XML Data

Next, we are going to display the data in our XML file by creating a table and adding the three buttons to our add, edit and delete functions. If you created a new XML file, the list/table will be empty at first till member data will be added.

Create a new file, name it as index.php

and paste the below codes.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CRUD Operation in XML Files using PHP</title>
  6. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">CRUD Operation in XML Files using PHP</h1>
  11. <div class="row">
  12. <div class="col-sm-8 col-sm-offset-2">
  13. <a href="#addnew" class="btn btn-primary" data-toggle="modal"><span class="glyphicon glyphicon-plus"></span> New</a>
  14. <?php
  15. session_start

    (

    )

    ;
  16. if

    (

    isset

    (

    $_SESSION

    [

    'message'

    ]

    )

    )

    {
  17. ?>
  18. <div class="alert alert-info text-center" style="margin-top:20px;">
  19. <?php

    echo

    $_SESSION

    [

    'message'

    ]

    ;

    ?>
  20. </div>
  21. <?php

  22. unset

    (

    $_SESSION

    [

    'message'

    ]

    )

    ;
  23. }
  24. ?>
  25. <table class="table table-bordered table-striped" style="margin-top:20px;">
  26. <thead>
  27. <th>UserID</th>
  28. <th>Firstname</th>
  29. <th>Lastname</th>
  30. <th>Address</th>
  31. <th>Action</th>
  32. </thead>
  33. <tbody>
  34. <?php
  35. //load xml file
  36. $file

    =

    simplexml_load_file

    (

    'files/members.xml'

    )

    ;

  37. foreach

    (

    $file

    ->

    user

    as

    $row

    )

    {
  38. ?>
  39. <tr>
  40. <td><?php

    echo

    $row

    ->

    id

    ;

    ?>

    </td>
  41. <td><?php

    echo

    $row

    ->

    firstname

    ;

    ?>

    </td>
  42. <td><?php

    echo

    $row

    ->

    lastname

    ;

    ?>

    </td>
  43. <td><?php

    echo

    $row

    ->

    address

    ;

    ?>

    </td>
  44. <td>
  45. <a href="#edit_<?php

    echo

    $row

    ->

    id

    ;

    ?>

    " data-toggle="modal" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-edit"></span> Edit</a>
  46. <a href="#delete_<?php

    echo

    $row

    ->

    id

    ;

    ?>

    " data-toggle="modal" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span> Delete</a>
  47. </td>
  48. <?php

    include

    (

    'edit_delete_modal.php'

    )

    ;

    ?>
  49. </tr>
  50. <?php
  51. }

  52. ?>
  53. </tbody>
  54. </table>
  55. </div>
  56. </div>
  57. </div>
  58. <?php

    include

    (

    'add_modal.php'

    )

    ;

    ?>
  59. <script src="jquery.min.js"></script>
  60. <script src="bootstrap/js/bootstrap.min.js"></script>
  61. </body>
  62. </html>

Creating our Modals

Next, we are going to create the modals for our add, edit and delete.

Create the ff files:

add_modal.php


  1. <!--

    Add New

    -->
  2. <

    div class

    =

    "modal fade"

    id=

    "addnew"

    tabindex=

    "-1"

    role=

    "dialog"

    aria-

    labelledby=

    "myModalLabel"

    aria-

    hidden=

    "true"

    >
  3. <

    div class

    =

    "modal-dialog"

    >
  4. <

    div class

    =

    "modal-content"

    >
  5. <

    div class

    =

    "modal-header"

    >
  6. <

    button type=

    "button"

    class

    =

    "close"

    data-

    dismiss=

    "modal"

    aria-

    hidden=

    "true"

    >&

    times;</

    button>
  7. <

    center><

    h4 class

    =

    "modal-title"

    id=

    "myModalLabel"

    >

    Add New</

    h4></

    center>
  8. </

    div>
  9. <

    div class

    =

    "modal-body"

    >
  10. <

    div class

    =

    "container-fluid"

    >
  11. <

    form method=

    "POST"

    action=

    "add.php"

    >
  12. <

    div class

    =

    "row form-group"

    >
  13. <

    div class

    =

    "col-sm-2"

    >
  14. <

    label class

    =

    "control-label"

    style=

    "position:relative; top:7px;"

    >

    ID:</

    label>
  15. </

    div>
  16. <

    div class

    =

    "col-sm-10"

    >
  17. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "id"

    >
  18. </

    div>
  19. </

    div>
  20. <

    div class

    =

    "row form-group"

    >
  21. <

    div class

    =

    "col-sm-2"

    >
  22. <

    label class

    =

    "control-label"

    style=

    "position:relative; top:7px;"

    >

    Firstname:</

    label>
  23. </

    div>
  24. <

    div class

    =

    "col-sm-10"

    >
  25. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "firstname"

    >
  26. </

    div>
  27. </

    div>
  28. <

    div class

    =

    "row form-group"

    >
  29. <

    div class

    =

    "col-sm-2"

    >
  30. <

    label class

    =

    "control-label"

    style=

    "position:relative; top:7px;"

    >

    Lastname:</

    label>
  31. </

    div>
  32. <

    div class

    =

    "col-sm-10"

    >
  33. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "lastname"

    >
  34. </

    div>
  35. </

    div>
  36. <

    div class

    =

    "row form-group"

    >
  37. <

    div class

    =

    "col-sm-2"

    >
  38. <

    label class

    =

    "control-label"

    style=

    "position:relative; top:7px;"

    >

    Address:</

    label>
  39. </

    div>
  40. <

    div class

    =

    "col-sm-10"

    >
  41. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "address"

    >
  42. </

    div>
  43. </

    div>
  44. </

    div>
  45. </

    div>
  46. <

    div class

    =

    "modal-footer"

    >
  47. <

    button type=

    "button"

    class

    =

    "btn btn-default"

    data-

    dismiss=

    "modal"

    ><

    span class

    =

    "glyphicon glyphicon-remove"

    ></

    span>

    Cancel</

    button>
  48. <

    button type=

    "submit"

    name=

    "add"

    class

    =

    "btn btn-primary"

    ><

    span class

    =

    "glyphicon glyphicon-floppy-disk"

    ></

    span>

    Save</

    a>
  49. </

    form>
  50. </

    div>

  51. </

    div>
  52. </

    div>
  53. </

    div>

edit_delete_modal.php


  1. <!-- Edit -->
  2. <div class="modal fade" id="edit_<?php

    echo

    $row

    ->

    id

    ;

    ?>

    " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  3. <div class="modal-dialog">
  4. <div class="modal-content">
  5. <div class="modal-header">
  6. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  7. <center><h4 class="modal-title" id="myModalLabel">Edit Member</h4></center>
  8. </div>
  9. <div class="modal-body">
  10. <div class="container-fluid">
  11. <form method="POST" action="edit.php">
  12. <div class="row form-group">
  13. <div class="col-sm-2">
  14. <label class="control-label" style="position:relative; top:7px;">ID:</label>
  15. </div>
  16. <div class="col-sm-10">
  17. <input type="text" class="form-control" name="id" value="<?php

    echo

    $row

    ->

    id

    ;

    ?>

    " readonly>
  18. </div>
  19. </div>
  20. <div class="row form-group">
  21. <div class="col-sm-2">
  22. <label class="control-label" style="position:relative; top:7px;">Firstname:</label>
  23. </div>
  24. <div class="col-sm-10">
  25. <input type="text" class="form-control" name="firstname" value="<?php

    echo

    $row

    ->

    firstname

    ;

    ?>

    ">
  26. </div>
  27. </div>
  28. <div class="row form-group">
  29. <div class="col-sm-2">
  30. <label class="control-label" style="position:relative; top:7px;">Lastname:</label>
  31. </div>
  32. <div class="col-sm-10">
  33. <input type="text" class="form-control" name="lastname" value="<?php

    echo

    $row

    ->

    lastname

    ;

    ?>

    ">
  34. </div>
  35. </div>
  36. <div class="row form-group">
  37. <div class="col-sm-2">
  38. <label class="control-label" style="position:relative; top:7px;">Address:</label>
  39. </div>
  40. <div class="col-sm-10">
  41. <input type="text" class="form-control" name="address" value="<?php

    echo

    $row

    ->

    address

    ;

    ?>

    ">
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. <div class="modal-footer">
  47. <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
  48. <button type="submit" name="edit" class="btn btn-success"><span class="glyphicon glyphicon-check"></span> Update</a>
  49. </form>
  50. </div>

  51. </div>
  52. </div>
  53. </div>

  54. <!-- Delete -->
  55. <div class="modal fade" id="delete_<?php

    echo

    $row

    ->

    id

    ;

    ?>

    " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  56. <div class="modal-dialog">
  57. <div class="modal-content">
  58. <div class="modal-header">
  59. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  60. <center><h4 class="modal-title" id="myModalLabel">Delete Member</h4></center>
  61. </div>
  62. <div class="modal-body">
  63. <p class="text-center">Are you sure you want to Delete</p>
  64. <h2 class="text-center"><?php

    echo

    $row

    ->

    firstname

    .

    ' '

    .

    $row

    ->

    lastname

    ;

    ?>

    </h2>
  65. </div>
  66. <div class="modal-footer">
  67. <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
  68. <a href="delete.php?id=<?php

    echo

    $row

    ->

    id

    ;

    ?>

    " class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Yes</a>
  69. </div>

  70. </div>
  71. </div>
  72. </div>

Creating our Add Script

Next, we are going to create our add script to add a new item to our XML file.

Create a new file, name it as add.php

and paste the below codes.

  1. <?php
  2. session_start

    (

    )

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'add'

    ]

    )

    )

    {
  4. //open xml file
  5. $users

    =

    simplexml_load_file

    (

    'files/members.xml'

    )

    ;
  6. $user

    =

    $users

    ->

    addChild

    (

    'user'

    )

    ;
  7. $user

    ->

    addChild

    (

    'id'

    ,

    $_POST

    [

    'id'

    ]

    )

    ;
  8. $user

    ->

    addChild

    (

    'firstname'

    ,

    $_POST

    [

    'firstname'

    ]

    )

    ;
  9. $user

    ->

    addChild

    (

    'lastname'

    ,

    $_POST

    [

    'lastname'

    ]

    )

    ;
  10. $user

    ->

    addChild

    (

    'address'

    ,

    $_POST

    [

    'address'

    ]

    )

    ;
  11. // Save to file
  12. // file_put_contents('files/members.xml', $users->asXML());
  13. // Prettify / Format XML and save
  14. $dom

    =

    new

    DomDocument(

    )

    ;
  15. $dom

    ->

    preserveWhiteSpace

    =

    false

    ;
  16. $dom

    ->

    formatOutput

    =

    true

    ;
  17. $dom

    ->

    loadXML

    (

    $users

    ->

    asXML

    (

    )

    )

    ;
  18. $dom

    ->

    save

    (

    'files/members.xml'

    )

    ;
  19. // Prettify / Format XML and save

  20. $_SESSION

    [

    'message'

    ]

    =

    'Member added successfully'

    ;
  21. header

    (

    'location: index.php'

    )

    ;
  22. }
  23. else

    {
  24. $_SESSION

    [

    'message'

    ]

    =

    'Fill up add form first'

    ;
  25. header

    (

    'location: index.php'

    )

    ;
  26. }
  27. ?>

Creating our Update Script

Next, we are going to create our update script by creating a new file, name it as edit.php

and paste the below codes.

  1. <?php
  2. session_start

    (

    )

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'edit'

    ]

    )

    )

    {
  4. $users

    =

    simplexml_load_file

    (

    'files/members.xml'

    )

    ;
  5. foreach

    (

    $users

    ->

    user

    as

    $user

    )

    {
  6. if

    (

    $user

    ->

    id

    ==

    $_POST

    [

    'id'

    ]

    )

    {
  7. $user

    ->

    firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  8. $user

    ->

    lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  9. $user

    ->

    address

    =

    $_POST

    [

    'address'

    ]

    ;
  10. break

    ;
  11. }
  12. }

  13. file_put_contents

    (

    'files/members.xml'

    ,

    $users

    ->

    asXML

    (

    )

    )

    ;
  14. $_SESSION

    [

    'message'

    ]

    =

    'Member updated successfully'

    ;
  15. header

    (

    'location: index.php'

    )

    ;
  16. }
  17. else

    {
  18. $_SESSION

    [

    'message'

    ]

    =

    'Select member to edit first'

    ;
  19. header

    (

    'location: index.php'

    )

    ;
  20. }

  21. ?>

Creating our Delete Script

Lastly, we create our delete script by creating a new file, name it as delete.php

and paste the below codes.

  1. <?php
  2. session_start

    (

    )

    ;
  3. $id

    =

    $_GET

    [

    'id'

    ]

    ;

  4. $users

    =

    simplexml_load_file

    (

    'files/members.xml'

    )

    ;

  5. //we're are going to create iterator to assign to each user
  6. $index

    =

    0

    ;
  7. $i

    =

    0

    ;

  8. foreach

    (

    $users

    ->

    user

    as

    $user

    )

    {
  9. if

    (

    $user

    ->

    id

    ==

    $id

    )

    {
  10. $index

    =

    $i

    ;
  11. break

    ;
  12. }
  13. $i

    ++;
  14. }

  15. unset

    (

    $users

    ->

    user

    [

    $index

    ]

    )

    ;
  16. file_put_contents

    (

    'files/members.xml'

    ,

    $users

    ->

    asXML

    (

    )

    )

    ;

  17. $_SESSION

    [

    'message'

    ]

    =

    'Member deleted successfully'

    ;
  18. header

    (

    'location: index.php'

    )

    ;

  19. ?>

DEMO

And that's the end of this tutorial. Test the simple web you made on your end and if there's an error occurred, kindly recheck the code in each file or you can also download the working source code that I have created in this tutorial.

I hope this will help you with what you are looking for and you'll find this tutorial useful for your future PHP Projects. Explore more on this website for more tutorials and free source codes.

Happy Coding :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


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

452,292

323,526

323,535

Top