Hi
Since someone in the office decided we needed to introduce some XML into our system, I'd like to ask a question with regards to the following code..
I've looked around at various scripts and would like to modify the following as I prefer the way the XML structure is created (as opposed to manually declaring each node) as an initial form handler.
It uses the element structure of the form names to build the nodes.
The problem is, if I try to build an XML string with this (replacing the echo commands), the layout is wrong.
formhandler example: (handler.php)
<?php XMLFormdata($_POST);
function XMLFormdata($saFormdata, $iLevel = 1) {
echo (($iLevel==1)?"<formdata>\n":"");
while( list( $sElement, $sValue ) = each( $saFormdata ) ) {
echo str_repeat("\t",$iLevel)."<".((is_int($sElement))?"count":"").$sElement.">";
if( is_array($sValue) ) {
echo "\n";
XMLFormdata( $sValue, $iLevel + 1 );
echo str_repeat("\t",$iLevel);
} else {
echo $sValue;
} // if/else
echo "</".((is_int($sElement))?"count":"").$sElement.">\n";
} // while
echo (($iLevel==1)?"</formdata>\n":"");
} // function XMLFormdata
?>
Example HTML Form (verified with java before submission on the final form)
<FORM action="handler.php" method=post>
<input type="text" name="Header[Field1]">
<input type="text" name="Header[Field2]">
<input type="text" name="Header[Field3]">
<input type="text" name="Header[Field4]">
<input type="text" name="Header[Field5]">
<input type="text" name="Header[Field6]">
<input type="text" name="Header[Field7]">
<input type="text" name="Header[Field8]">
<input type="text" name="Details[Personal][Field1]">
<input type="text" name="Details[Personal][Field2]">
<BR>
<input type="submit" value="submit">
</form>
Viewing the source of the result gives the following:
<formdata>
<Header>
<Field1>1</Field1>
<Field2>2</Field2>
<Field3>3</Field3>
<Field4>4</Field4>
<Field5>5</Field5>
<Field6>6</Field6>
<Field7>7</Field7>
<Field8>8</Field8>
</Header>
<Details>
<Personal>
<Field1>9</Field1>
<Field2>10</Field2>
</Personal>
</Details>
</formdata>
(The numbers 1-10 were entered into the form)
The above XML is perfect and means I only have to specify the field names correctly, however I want to store that as a string so I can HTTP post it to another SSL server using fsockopen("ssl://".$hostname, $portnum, $errno, $errstr, $timeout);
No matter what I do, I cannot seem to maintain the order after removing the echo commands...
Thanks 🙂