Menu
Create Dynamic XML using PHP

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

PHP By Aug 13, 2021 No Comments

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>
            </select>
        </div>
    <div class="form-group">
        <textarea id="Message" name="Message" class="form-control"></textarea>
    </div>
<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>

</form>

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();
$xmlFileName="xmlManager.xml";
if(isset($_POST["submit"])){
    unset($_POST["submit"]);
    $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.

autoAssignerClass.php

<?php
require_once( dirname( __FILE__ ) . '/../alert/alertClass.php' );
class autoAssignerClass {
public function getXmlDetails($data,$filename){
        if(file_exists($filename)){
            $alert=$this->updateEntryToXML($data,$filename);
        }
        else
        {
            $alert=$this->createFirstTimeXML($data,$filename);
        }
    }

private function createFirstTimeXML($data,$filename){
        $xmlDom=new DOMDocument();
        $xmlDom->version="1.0";
        $xmlDom->encoding='utf-8';
        $xmlDom->formatOutput=true;
        $xmlFileName=$filename;
        $root=$xmlDom->createElement("AutoManager");
        $xmNode=$xmlDom->createElement("Type");
        $xmlDetailsNode=$xmlDom->createElement("Name",$data["Name"]);
        $xmNode->appendChild($xmlDetailsNode);
        $xmlDetailsNode=$xmlDom->createElement("Message",$data["Message"]);
        $xmNode->appendChild($xmlDetailsNode);
        $root->appendChild($xmNode);
        $xmlDom->appendChild($root);
        $xmlDom->save($xmlFileName);
        return "Sms has been added,success";
    }
    
    private function updateEntryToXML($data,$filename){
        $dom = new DOMDocument;
        $dom->load($filename);
        $dom->encoding='utf-8';
        $dom->formatOutput=true;
        $xmlArr= $dom->getElementsByTagName('Name');
        foreach ($xmlArr as $value) {
            $portalArr[]=$value->nodeValue;
        }
        if(in_array($data["Name"], $portalArr)){
            return "Sms already exist,warning";
        }
        else
        {
            $xmNode = $dom->createElement("Type", "");
            $empName = $dom->createElement("Name", $data["Name"]);
            $xmNode->appendChild($empName);
            $empPortal = $dom->createElement("Message", $data["Message"]);
            $xmNode->appendChild($empPortal);
            $dom->getElementsByTagName("AutoManager")->item(0)->appendChild($xmNode);
            $dom->save($filename);
            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.

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

No Comments

Leave a comment

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