[Solved] HTML form to XML via PHP
Posted: Tue May 01, 2007 4:04 pm
So I'm updating/remaking a website for work which involves collecting information through an html form that submits to a mailing script, and I think to myself, "Hey, why should that poor admin spend half her morning copy-pasting 20 form fields into excell, when excell can just import xml?"
And so I tried tweaking the mail code.
And found that 80% of the script wasn't needed.
So I was like, "Dude, this code is from 2002, we can do better, we can do PHP."
And thus I wrote the following code. And it works. However, being as this is the first time I've made a purposeful PHP script/code/page I was hoping someone would look at it and help me avoid a catastropic mistake. You know, I kinda like the job and everything. Like say, "You fool! As soon as two people use your form at the same time you will corrupt the file and loose alll data!" (Although I think that by setting fopen() to a+ I should be okay with that one).
Please to be reviewing:
How it's supposed to work:
Open the .xml file already on the server, named "$formnameFile.xml". Then get rid of the closing tag for the root element of the file. Then write all of the values from "_POST", wrapped in xml opening and closing tags to $formnameFile.xml. Close the file.
Thanks for your help![/i]
And so I tried tweaking the mail code.
And found that 80% of the script wasn't needed.
So I was like, "Dude, this code is from 2002, we can do better, we can do PHP."
And thus I wrote the following code. And it works. However, being as this is the first time I've made a purposeful PHP script/code/page I was hoping someone would look at it and help me avoid a catastropic mistake. You know, I kinda like the job and everything. Like say, "You fool! As soon as two people use your form at the same time you will corrupt the file and loose alll data!" (Although I think that by setting fopen() to a+ I should be okay with that one).
Please to be reviewing:
Code: Select all
<?php
$myFile;
$stringData;
$formName;
foreach($_POST as $key=>$value){
if($key=="formName"){
$formName = $value;
}elseif($key!="submit"){
$stringData = $stringData."<".$key.">\n\t\t".$value."</".$key.">\n";
}
}
$myFile = "../data/$formName"."File.xml";
$stringData= "<$formName>".$stringData."</$formName>\n</$formName"."Register>";
$fh = fopen($myFile, "a+") or die("can't open file");
$chop= filesize($myFile);
$chop= $chop-(strlen($formName)+11);
ftruncate($fh, $chop);
fwrite($fh, $stringData);
fclose($fh);
?>Open the .xml file already on the server, named "$formnameFile.xml". Then get rid of the closing tag for the root element of the file. Then write all of the values from "_POST", wrapped in xml opening and closing tags to $formnameFile.xml. Close the file.
Thanks for your help![/i]