Create Dynamic XML Using PHP Part -1 | Create Dropdown Using XML

By | August 13, 2021
Create Dynamic XML using PHP

Create Dynamic XML using PHP is our new topic, Hello friends, this time i came up with interesting dynamic use of xml instead of Database. Many times we need to save some values and we do not need to play with database features. This XML facility can help you to make things dynamic like: Website Slider, Saving form values, Crating dynamic dropdown box etc. These are fast to use as you dont have to connect to the database.

In this tutorial i will just create the xml and save the values to the xml. In next we will check the duplicate entry and delete the entry. So lets start……

If you are interested in creating dropdown based image uploaded click here to check it out

First of all we will create a form through which we will receive our values. So lets start our form building.

<form autocomplete="off" action="" method="post">
<div class="form-group">
            <label>Choose SMS Type</label>
            <select class="form-control input-sm" id="Name" name="Name">
                <option value="">Select</option>
                <option value="Bill">New Billing</option>
                <option value="Success">Billing Success</option>
                <option value="Error">Billing Error</option>
    <div class="form-group">
        <textarea id="Message" name="Message" class="form-control"></textarea>
<button type="submit" id="submit" name="submit" class="btn btn-space btn-primary">Add SMS</button>
      <button type="reset" class="btn btn-space btn-default">Reset</button>


This form is related to saving sms templates for which we are going to use in our application.  After submit it will give 2 values 1. Name of Sms 2. Content of SMS. After the submit we will send our values to our is_set function.

is_set function

include_once '/auto/autoAssignerClass.php';
$autoAssigner=new autoAssignerClass();
    $alert=$autoAssigner->getXmlDetails($_POST, $xmlFileName);

In the above code i am not using any kind of filtration or checking of the values so if you are using the code then please do that so you wont be in trouble. Now you have have the values and as you can see i am using a function getXmlDetails with 2 parameters 1 will have your form values and 2nd will have the file name this function belongs to the autoAssignerClass. After getting the values we will submit the values to our xml file using our class.


require_once( dirname( __FILE__ ) . '/../alert/alertClass.php' );
class autoAssignerClass {
public function getXmlDetails($data,$filename){

private function createFirstTimeXML($data,$filename){
        $xmlDom=new DOMDocument();
        return "Sms has been added,success";
    private function updateEntryToXML($data,$filename){
        $dom = new DOMDocument;
        $xmlArr= $dom->getElementsByTagName('Name');
        foreach ($xmlArr as $value) {
        if(in_array($data["Name"], $portalArr)){
            return "Sms already exist,warning";
            $xmNode = $dom->createElement("Type", "");
            $empName = $dom->createElement("Name", $data["Name"]);
            $empPortal = $dom->createElement("Message", $data["Message"]);
            return "Sms has been added,success";

In the above code you can see that we are using 3 functions. First function getXmlDetails() will have your values which is coming from the form. Now this function will call the next function based upon the details. If its first time and your xml file is not created then it will call the createFirstTimeXML() function with details. Now if we have already added the first entry or created the xml then our updateEntryToXML() function will run.

If you are posting the same type of sms then will give you the error of “already exists” otherwise it will just update the xml node to the already created xml. Use this code and create dynamic xml using php

In the next tutorial you will see the Listing of data stored and deletion of data stored. Hope you will like the tutorial please share it and use the code if you face any issue then please comment.

Leave a Reply

Your email address will not be published. Required fields are marked *