Home > Development, English, Misc, OSS Solutions, php, Tech Crunch > phpxml2form, or how to quick-build an HTML form from any XML document

phpxml2form, or how to quick-build an HTML form from any XML document

I’ve been trying to find the kind of PHP library that allows you to quick-generate an HTML form a basic XML structure. Apparently there is no such thing, so here you can find some code that will help you do that (you still have to build the form tags from outside the function). This code is LGPL

/**
 * This function builds and prints an HTML form from a given XML Element (loaded by simplexml_load_file())
 * @param    object    A SimpleXML object
 * @param    int        Iteration (to know what level of recursivity we're in and print margins accordingly)
 * @param    string    The basename (prefix) to use for the input names
 * @example
 * $xml = simplexml_load_file($xml_file_path);
 * echo '<form action="" method="POST">';
 * xml2form($xml);
 * echo '<input type="submit" name="submit" value="Submit">';
 * echo '</form>';
 */
function xml2form($xml,$iter=0,$basename='') {
 global $attributes_non_editable; // an array of elements to "disable" in the form
 $s = '- '; // the symbol to repeat to show a tree-like structure
 foreach ($xml->children() as $c) {
  $n = $c->getName();
  $id = 0;
  $attribs = '';
  if ($c->attributes()) {
   foreach ($c->attributes() as $k => $e) {
    if ($k == 'id') {
     $id = $e;
    }
    $attribs .= str_repeat($s,$iter+1).'<label for="'.$basename.'['.$n.']['.$id.']['.trim($k).']">'.trim($k).'</label>';
    $dis = '';
    if (in_array($k, $attributes_non_editable)) {
     $dis = 'disabled="disabled"';
    }
    $attribs .= '<input type="text" name="'.$basename.'['.$n.']['.$id.']['.trim($k).']'.'" value="'.trim($e).'" '.$dis.' />';
    $attribs .= '<br />';
   }            
  }
  if (!empty($n)) {
   echo str_repeat($s,$iter).'<label for="'.$basename.'['.$n.']['.$id.']'.'">'.$n.'</label>';
   echo '<br />';
  }
  if ($c->children()) {
   echo $attribs;
   if (empty($basename)) {
    xml2form($c,$iter+1,$n.'['.$id.']');
   } else {
    xml2form($c,$iter+1,$basename.'['.$n.']['.$id.']');
   }
   echo '<br />';
  } else {
   if (isset($c[0])) {
    echo '<input type="text" name="'.$basename.'['.$n.']['.$id.']'.'" value="'.trim($c[0]).'" /><br />';
   }
   echo $attribs;
  }
  echo "\n";
 }
 if ($iter == 2) { echo '<hr />';}
 return true;
}

Many things can be improved (styling-wise) and it would be better with a function to wite the XML as well, but this should be enough for now…

  1. rxp2007
    October 2, 2013 at 3:53 pm

    Its a good code, but how can i read post variables to save another xml file ?

  2. rxp2007
    October 2, 2013 at 4:04 pm

    I change your code like that:
    <?php
    /**
    * This function builds and prints an HTML form from a given XML Element (loaded by simplexml_load_file())
    * @param object A SimpleXML object
    * @param int Iteration (to know what level of recursivity we're in and print margins accordingly)
    * @param string The basename (prefix) to use for the input names
    * @example
    * $xml = simplexml_load_file($xml_file_path);
    * echo '’;
    * xml2form($xml);
    * echo ”;
    * echo ”;
    */

    function xml2form($xml,$iter=0,$basename=”) {
    global $attributes_non_editable; // an array of elements to “disable” in the form
    $s = ‘- ‘; // the symbol to repeat to show a tree-like structure
    foreach ($xml->children() as $c) {
    $n = $c->getName();
    $id = 0;
    $attribs = ”;
    if ($c->attributes()) {
    foreach ($c->attributes() as $k => $e) {
    if ($k == ‘id’) {
    $id = $e;
    }
    $attribs .= str_repeat($s,$iter+1).”.trim($k).”;
    $dis = ”;
    if (in_array($k, $attributes_non_editable)) {
    $dis = ‘disabled=”disabled”‘;
    }
    $attribs .= ”;
    //$attribs .= ”;
    }
    }
    if (!empty($n)) {
    echo ”;
    echo “\n”;
    echo ”;
    echo “\n”;
    echo str_repeat($s,$iter).”.$n.”;
    echo “\n”;
    //echo ”;
    echo ”;
    echo “\n”;
    }
    if ($c->children()) {
    echo $attribs;
    if (empty($basename)) {
    xml2form($c,$iter+1,$n.'[‘.$id.’]’);
    }
    else {
    xml2form($c,$iter+1,$basename.'[‘.$n.’][‘.$id.’]’);
    }
    //echo ”;

    }
    else {
    if (isset($c[0])) {
    echo ”;
    echo “\n”;
    echo ”;
    echo “\n”;
    echo ”;
    }
    echo $attribs;
    }
    echo “\n”;
    //echo ”;
    }

    if ($iter == 2) { echo ”;}
    return true;
    }

    ?>

    XML para formulário

    $(document).ready(function() {
    $(“#submit”).on(“click”,function(){
    xml = update();
    $.ajax({
    url : “saveXml.php”,
    type:”post”,
    data : xml,
    contentType: “text/xml”,
    success : function(response){
    alert(“xml saved successfully”);
    }
    });
    });
    }

    Exemplo 1
    Teste
    <?php
    $xml_file_path="teste1b.xml";
    $xml = simplexml_load_file($xml_file_path);
    echo '’;
    echo ”;
    xml2form($xml);
    echo ”;
    echo ”;
    echo ”;

    ?>

    The style.css file is like this:
    @CHARSET “ISO-8859-1”;
    body{
    color:#666;
    background-color:#f3f3f3;
    }

    a{
    color:#2CA8DD;
    text-decoration:none;
    border-bottom:1px dotted #999;
    }
    a:hover{
    color:#2CDDB7;
    text-decoration:none;
    border-bottom:1px dotted #999;
    }
    .errorfield{
    border:1px solid #ff0000;
    }
    input[type=submit], input[type=reset],button{
    padding:5px 20px;
    background-color:#2CDDB7;
    color:#fff;
    font-weight:bold;
    cursor:pointer
    }

    input[type=radio]{
    padding:1px;

    }
    label {
    /*width:200px;
    display: block;*/
    }
    input, select, textarea{
    background-color:#eee;
    color:#666;
    border:1px solid #ddd;
    padding:4px;
    width:200px;
    }
    table td{
    padding:8px;
    border-top:1px solid #f3f3f3;
    width:200px;
    }
    th{
    padding:8px;
    border-top:1px solid #f3f3f3;
    width:90px;
    text-align: left;
    }
    #container{
    background-color:#fff;
    width:auto;
    margin:20px auto;
    border:10px solid #ddd;
    padding:15px;
    box-shadow:0 0 30px #D8D8D8;
    }
    .error{
    color:#ff0000;
    }

    I want to make a saveXml.php, but how to read names and values from dynamic xml file?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: