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

PHP - Update Data in XML File

xISucxk

Environment Manager
X Rep
0
0
0
Rep
0
X Vouches
0
0
0
Vouches
0
Posts
66
Likes
69
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Update Data in XML File using PHP. This code will launch a bootstrap modal to update a specific XML data in the table. The code use file_put_contents() to save the XML data after the php loop assign the designate key value of POST data. This is a user-friendly kind of program feel free to modify it.

We will be using XML as a markup language that utilize in php as a HTML data. It is designed to store and transport data that can be manipulated within the local server.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, this is the link for the jquery that i used in this tutorial https://jquery.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Update Data in XML File</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <table class="table table-bordered table-striped" style="margin-top:20px;">
  18. <thead class="alert-info">
  19. <th>Member ID</th>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Address</th>
  23. <th>Action</th>
  24. </thead>
  25. <tbody>
  26. <?php
  27. $file

    =

    simplexml_load_file

    (

    'members.xml'

    )

    ;
  28. foreach

    (

    $file

    ->

    member

    as

    $row

    )

    {
  29. ?>
  30. <tr>
  31. <td><?php

    echo

    $row

    ->

    mem_id

    ;

    ?>

    </td>
  32. <td><?php

    echo

    $row

    ->

    firstname

    ;

    ?>

    </td>
  33. <td><?php

    echo

    $row

    ->

    lastname

    ;

    ?>

    </td>
  34. <td><?php

    echo

    $row

    ->

    address

    ;

    ?>

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

    echo

    $row

    ->

    mem_id

    ;

    ?>

    " data-toggle="modal" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit</a></td>
  36. <div class="modal fade" id="edit_<?php

    echo

    $row

    ->

    mem_id

    ;

    ?>

    " aria-hidden="true">
  37. <div class="modal-dialog">
  38. <div class="modal-content">
  39. <form method="POST" action="update.php">
  40. <div class="modal-header">
  41. <center><h4 class="modal-title" id="myModalLabel">Edit Member</h4></center>
  42. </div>
  43. <div class="modal-body">
  44. <div class="col-md-2"></div>
  45. <div class="col-md-8">
  46. <div class="form-group">
  47. <label >Member ID</label>
  48. <input type="text" class="form-control" name="id" value="<?php

    echo

    $row

    ->

    mem_id

    ;

    ?>

    " readonly="readonly" />
  49. </div>
  50. <div class="form-group">
  51. <label >Firstname:</label>
  52. <input type="text" class="form-control" name="firstname" value="<?php

    echo

    $row

    ->

    firstname

    ;

    ?>

    ">
  53. </div>
  54. <div class="form-group">
  55. <label>Lastname:</label>
  56. <input type="text" class="form-control" name="lastname" value="<?php

    echo

    $row

    ->

    lastname

    ;

    ?>

    ">
  57. </div>
  58. <div class="form-group">
  59. <label>Address:</label>
  60. <input type="text" class="form-control" name="address" value="<?php

    echo

    $row

    ->

    address

    ;

    ?>

    ">
  61. </div>
  62. </div>
  63. </div>
  64. <br style="clear:both;"/>
  65. <div class="modal-footer">
  66. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  67. <button name="edit" class="btn btn-warning"><span class="glyphicon glyphicon-save"></span> Update</a>
  68. </div>
  69. </form>
  70. </div>
  71. </div>
  72. </div>
  73. </tr>
  74. <?php
  75. }

  76. ?>
  77. </tbody>
  78. </table>
  79. </div>
  80. <script src="js/jquery-3.2.1.min.js"></script>
  81. <script src="js/bootstrap.min.js"></script>
  82. </body>
  83. </html>

Creating the Main Function

This code contains the main function of the application. This code will automatically update the xml file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
members.xml

xml version="1.0"?

1

John

Smith

New York

2

Regina

Wilson

Jurassic Park

update.php
  1. <?php
  2. if

    (

    ISSET

    (

    $_POST

    [

    'edit'

    ]

    )

    )

    {
  3. $members

    =

    simplexml_load_file

    (

    'members.xml'

    )

    ;
  4. foreach

    (

    $members

    ->

    member

    as

    $member

    )

    {
  5. if

    (

    $member

    ->

    mem_id

    ==

    $_POST

    [

    'id'

    ]

    )

    {
  6. $member

    ->

    firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  7. $member

    ->

    lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  8. $member

    ->

    address

    =

    $_POST

    [

    'address'

    ]

    ;
  9. break

    ;
  10. }
  11. }

  12. file_put_contents

    (

    'members.xml'

    ,

    $members

    ->

    asXML

    (

    )

    )

    ;
  13. header

    (

    "location: index.php"

    )

    ;
  14. }
  15. ?>

There you have it we successfully created Update Data in XML File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


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

452,292

323,341

323,350

Top