PHP - Simple Delete Data In XML File

Submitted by: 
Visitors have accessed this post 157 times.

In this tutorial we will create a Simple Delete Data In XML File using PHP. This code will launch a modal dialog to warning the user about deleting the data in the xml file, when the delete is click the data is automatically deleted in the xml file. The code use unset() function to delete a specific data in the xml file base on the index position after clicking the button. This is a user-friendly kind of program feel free to modify it.

We will be using XML as a markup language that defines a set of rules for encoding documents in a format that is both readable in a certain way. 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

And, this is the link for the bootstrap that i used for the layout design

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>
  3. <head>
  4. <meta charset="utf-8" name="viewport" content="width=device-width, intial-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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Delete Data In XML File</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <table class="table table-bordered table-striped" style="margin-top:20px;">
  19. <thead class="alert-info">
  20. <th>Student ID</th>
  21. <th>Firstname</th>
  22. <th>Lastname</th>
  23. <th>Address</th>
  24. <th>Action</th>
  25. </thead>
  26. <tbody>
  27. <?php
  28. $file = simplexml_load_file('students.xml');
  30. foreach($file->student as $row){
  31. ?>
  32. <tr>
  33. <td><?php echo $row->stud_id; ?></td>
  34. <td><?php echo $row->firstname; ?></td>
  35. <td><?php echo $row->lastname; ?></td>
  36. <td><?php echo $row->address; ?></td>
  37. <td><a href="#delete_<?php echo $row->stud_id; ?>" data-toggle="modal" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete</a></td>
  38. </tr>
  39. <div class="modal fade" id="delete_<?php echo $row->stud_id; ?>" role="dialog" aria-hidden="true">
  40. <div class="modal-dialog">
  41. <div class="modal-content">
  42. <div class="modal-header">
  43. <h4 class="modal-title">System Information</h4>
  44. </div>
  45. <div class="modal-body">
  46. <center><h3 class="text-danger">Are you sure you want to Delete this record?</h3></center>
  47. </div>
  48. <div class="modal-footer">
  49. <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  50. <a href="delete.php?id=<?php echo $row->stud_id; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Yes</a>
  51. </div>
  53. </div>
  54. </div>
  55. </div>
  56. <?php
  57. }
  58. ?>
  59. </tbody>
  60. </table>
  61. </div>
  62. <script src="js/jquery-3.2.1.min.js"></script>
  63. <script src="js/bootstrap.js"></script>
  64. </body>
  65. </html>

Creating the Main Function

This code contains the main function of the application. This code will remove the data in 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 delete.php

  1. <?php
  2. $id = $_GET['id'];
  4. $students = simplexml_load_file('students.xml');
  6. $position = 0;
  7. $i = 0;
  9. foreach($students->student as $student){
  10. if($student->id == $id){
  11. $position = $i;
  12. break;
  13. }
  14. $i++;
  15. }
  17. unset($students->student[$position]);
  18. file_put_contents('students.xml', $students->asXML());
  20. header('location: index.php');
  22. ?>

There you have it we successfully created Simple Delete 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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.