PHP - Store Data Inputs As XML File
Submitted by razormist on Tuesday, March 26, 2019 - 11:39.
In this tutorial we will create a Store Data Inputs As XML File using PHP. This code wil store html form data as a xml file when the user click the save button. The code use DomDocument to create a xml file, then append all the html form data as a child of the xml parent node. This is a user-friendly kind of program feel free to modify it.
We will be using XML as a HTML readable format string in order to utilize php code for appending the data. It is designed to store and transport data that can be manipulated within the local server.
There you have it we successfully created Store Data Inputs As 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!
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/.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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Store Data Inputs As XML File</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
- <br /><br />
- <table class="table table-bordered" >
- <thead class="alert-info">
- <tr>
- <th>Member ID</th>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Address</th>
- </tr>
- </thead>
- <tbody>
- <?php
- foreach($xml->member as $member){
- echo '
- <tr>
- <td>'.$member->mem_id.'</td>
- <td>'.$member->firstname.'</td>
- <td>'.$member->lastname.'</td>
- <td>'.$member->address.'</td>
- </tr>
- ';
- }
- ?>
- </tbody>
- </table>
- </div>
- <div class="modal fade" id="form_modal" aria-hidden="true">
- <div class="modal-dialog">
- <div class="modal-content">
- <form method="POST" action="save_member.php">
- <div class="modal-header">
- <h3 class="modal-title">Add Member</h3>
- </div>
- <div class="modal-body">
- <div class="col-md-2"></div>
- <div class="col-md-8">
- <div class="form-group">
- <label>Member ID</label>
- <input type="text" class="form-control" name="mem_id" required="required"/>
- </div>
- <div class="form-group">
- <label>Firstname</label>
- <input type="text" class="form-control" name="firstname" required="required"/>
- </div>
- <div class="form-group">
- <label>Lastname</label>
- <input type="text" class="form-control" name="lastname" required="required"/>
- </div>
- <div class="form-group">
- <label>Address</label>
- <input type="text" class="form-control" name="address" required="required"/>
- </div>
- </div>
- </div>
- <br style="clear:both;"/>
- <div class="modal-footer">
- <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
- <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </body>
- </html>
Creating the Main Function
This code contains the main function of the application. This code will save the data inputs to 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 save_member.php- <?php
- $member = $members->addChild('member');
- $member->addChild('mem_id', $_POST['mem_id']);
- $member->addChild('firstname', $_POST['firstname']);
- $member->addChild('lastname', $_POST['lastname']);
- $member->addChild('address', $_POST['address']);
- }else{
- $xml = new DomDocument("1.0", "UTF-8");
- $members= $xml->createElement("members");
- $members = $xml->appendChild($members);
- $member = $xml->createElement("member");
- $member = $members->appendChild($member);
- $mem_id = $xml->createElement("mem_id", $_POST['mem_id']);
- $mem_id = $member->appendChild($mem_id);
- $firstname = $xml->createElement("firstname", $_POST['firstname']);
- $firstname = $member->appendChild($firstname);
- $lastname = $xml->createElement("lastname", $_POST['lastname']);
- $lastname = $member->appendChild($lastname);
- $address = $xml->createElement("address", $_POST['address']);
- $address = $member->appendChild($address);
- $xml->FormatOutput = true;
- $string_value = $xml->saveXML();
- $xml->save("member.xml");
- }
- }
- ?>
Add new comment
- 205 views