Jiin
05-14-2010, 08:45 AM
Hello All, Thank in Advance!
I am using PHP5 server side to automatically generate an HTML form from an XML file. That part works! Then, using the same script and $_SERVER['PHP_SELF'] functionality to re execute the script when it is submitted I try to write the HTML form data back to an XML file......
I just can't seem to get the if(isset($_POST['submit'])) section of my code to execute. Any help is REALLY appreciated.
Problem script
------------------------------------
<?php
//Do we need to present a blank form or do we need to send submitted data to a XML file?
//If the form has already been submitted then send data to XML file
if(isset($_POST['submit']))
{
//HTML form data will be saved as XML file
//XML file will be named after HTML address field
//Convert spaces to underscores so we have no space in our file name
$newname = spaces2Underscore($_POST["address"]);
//Check if there is already an existing XML file with same name
$newxmlfile = "../properties/$newname.xml";
if (is_file("$newxmlfile")) {
die("Error, \"Name\" is already in use. Click your browsers \"Back\" button and try renaming.");
}
//Open file for writing
$fp = fopen($newxmlfile, "w") or die("Unable to create: $newxmlfile");
//Write data from HTML form to XML file
foreach ($_POST as $key => $value) {
$line ="<".$key.">".$value."</".$key.">\n";
//echo $line;
fputs($fp, "$line");
}
//Close file
fclose($fp);
}
//Only execute this portion of the code if form has not been submitted
//Use AddNewProperty.xml for template
$template = "../properties/propertytemplate.xml" ;
//Ensure tempalte exists
if (!is_file("$template")) {
die("Error, $template does not exist.");
}
//Open propertytemplate.xml
$xml = simplexml_load_file("$template");
//Use propertytemplate.xml to create HTML form
echo "Edit the information and click \"submit\" <br /><br />";
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<?php
foreach($xml->children() as $child) {
echo $child->getName() . ": " . $child . "<br />";
echo "<input type=\"text\" size=\"60\" name=\"$child->getName()\" value=\"$child\" /><br />";
echo "<br /><br />";
}
//echo "<input type=\"submit\" value=\"submit\" /><br />";
//echo "</form>";
?>
<input type="submit" value="submit" />
</form>
-----------------------------------------------
I am using PHP5 server side to automatically generate an HTML form from an XML file. That part works! Then, using the same script and $_SERVER['PHP_SELF'] functionality to re execute the script when it is submitted I try to write the HTML form data back to an XML file......
I just can't seem to get the if(isset($_POST['submit'])) section of my code to execute. Any help is REALLY appreciated.
Problem script
------------------------------------
<?php
//Do we need to present a blank form or do we need to send submitted data to a XML file?
//If the form has already been submitted then send data to XML file
if(isset($_POST['submit']))
{
//HTML form data will be saved as XML file
//XML file will be named after HTML address field
//Convert spaces to underscores so we have no space in our file name
$newname = spaces2Underscore($_POST["address"]);
//Check if there is already an existing XML file with same name
$newxmlfile = "../properties/$newname.xml";
if (is_file("$newxmlfile")) {
die("Error, \"Name\" is already in use. Click your browsers \"Back\" button and try renaming.");
}
//Open file for writing
$fp = fopen($newxmlfile, "w") or die("Unable to create: $newxmlfile");
//Write data from HTML form to XML file
foreach ($_POST as $key => $value) {
$line ="<".$key.">".$value."</".$key.">\n";
//echo $line;
fputs($fp, "$line");
}
//Close file
fclose($fp);
}
//Only execute this portion of the code if form has not been submitted
//Use AddNewProperty.xml for template
$template = "../properties/propertytemplate.xml" ;
//Ensure tempalte exists
if (!is_file("$template")) {
die("Error, $template does not exist.");
}
//Open propertytemplate.xml
$xml = simplexml_load_file("$template");
//Use propertytemplate.xml to create HTML form
echo "Edit the information and click \"submit\" <br /><br />";
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<?php
foreach($xml->children() as $child) {
echo $child->getName() . ": " . $child . "<br />";
echo "<input type=\"text\" size=\"60\" name=\"$child->getName()\" value=\"$child\" /><br />";
echo "<br /><br />";
}
//echo "<input type=\"submit\" value=\"submit\" /><br />";
//echo "</form>";
?>
<input type="submit" value="submit" />
</form>
-----------------------------------------------