<?php
// xml_on_the_fly.php
// RJM Programming
// June, 2018
// Thanks to The PHP Anthology by Harry Fuecks

  $formnav=false;
  $toptype="";
  $fromthenon="";

  foreach ($_POST as $name=>$val) {
    $formnav=true;
    if (htmlspecialchars($name) == "element1") {
      $dom=new DOMDocument('1.0'); //domxml_new_doc('1.0');
      $toptype=htmlspecialchars($val);
      $example=$dom->createElement($toptype); //create_element($toptype);
    } else if (htmlspecialchars($name) == "element2") {
      $fromthenon=htmlspecialchars($val);
    } else {
      $date=$dom->createElement($fromthenon); //create_element($fromthenon);
      $dateText=$dom->createTextNode(htmlspecialchars($val)); //create_text_node(htmlspecialchars($val));
      $date->appendChild($dateText); //append_child($dateText);
      $example->appendChild($date); //append_child($date);
    }
  }
  
  if ($formnav) {
    $dom->appendChild($example); //append_child($example);
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    header('Content-Type:text/xml');
    //echo $dom->saveXML(); //dump_mem();
    $xml_string = $dom->saveXML(); //dump_mem();
    echo $xml_string;
  } else {
    echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
var nextl=2;
var tr2='';
var xtdo=null;
function fixtopthis(tdo) {
  document.getElementById('elementn').innerHTML=tdo.value;
}
function fixthis(tdo) {
  document.getElementById('elem2').value=tdo.value;
  var divs=document.getElementsByTagName('div');
  for (idivs=0; idivs<divs.length; idivs++) {
   if (divs[idivs].className == 'divn') {
    divs[idivs].innerHTML=tdo.value;
   }
  }
  document.getElementById('element2td').innerHTML=tdo.value;
  trtwo=document.getElementById('tr2').innerHTML;
}
function postaddthis() {
  addnew(xtdo);
}
function addnew(tdo) {
  var thisl=eval(tdo.id.replace('element','').replace('val',''));
  if (eval(1 + thisl) > nextl) {
  nextl=eval(1 + thisl);
  thisl=2;
  document.getElementById('tr' + nextl).innerHTML=trtwo.replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl).replace('element' + thisl,'element' + nextl);
  document.getElementById('tr' + nextl).style.display='table-row';
  }
}
function lotsoftr() {
  trtwo=document.getElementById('tr2').innerHTML;
  for (var i=3; i<100; i++) {
    document.getElementById('mytbody').innerHTML+='<tr style=display:none; id=tr' + i + '><td colspan=7></td></tr>';
  }
}
function okd(event) { // thanks to https://stackoverflow.com/questions/15355948/preventing-tab-to-cycle-through-address-bar
  if (event.keyCode == 9 && event.shiftKey == false) {
    document.getElementById('element1').focus();
  }
}
function okdzero(event) { // thanks to https://stackoverflow.com/questions/15355948/preventing-tab-to-cycle-through-address-bar
  if (event.keyCode == 9 && event.shiftKey == true) {
    if (document.getElementById('element' + eval(1 + nextl) + 'val')) {
    document.getElementById('element' + eval(1 + nextl) + 'val').focus();
    } else {
    document.getElementById('element' + nextl + 'val').focus();
    }
  }
}
</script>
</head>
<body style='background-color: yellow;' onload='lotsoftr();'>
<h1>XML on the Fly</h1>
<h3>RJM Programming - June, 2018</h3>
<h3>Thanks to The PHP Anthology (Volume II: Applications) by Harry Fuecks</h3>
<form method=POST action=./xml_on_the_fly.php>
<table id=mytable style='width:100%;background-color:lightblue;'>
<thead id=myhead>
<tr><th colspan=3 align=right>&lt;</th><th><input type=text onkeydown='okdzero(event);' onblur=fixtopthis(this); style='width:100%;' name=element1 id=element1 value=example></input><input type=hidden name=element2 id=elem2 value=date></input></th><th colspan=3 align=left>&gt;</th></tr>
</thead>
<tbody id=mytbody>
<tr id=tr2><td align=right>&lt;</td><td id=element2td><input type=text onblur=fixthis(this); style='width:100%;' id=element2 value=date></input></td><td align=left>&gt;</td><td><input type=text onkeydown='okd(event);' onblur='xtdo=this; setTimeout(postaddthis,2000);' style='width:100%;' name=element2val id=element2val value='" . date('h:ia l jS M Y') . "'></input></td><td align=right>&lt;/</td><td><div class=divn>date</div></td><td align=left>&gt;</td></tr>
</tbody>
<tfoot id=myfoot>
<tr><th colspan=3 align=right>&lt;/</th><th><div id=elementn>example</div></th><th colspan=3 align=left>&gt;</th></tr>
</tfoot>
</table>
<input style='background-color:pink;width:100%;text-align:center;' type=submit value='Create XML'></input>
</form>
</body>
</html>    
";
  }
?>