Click to See Complete Forum and Search --> : $_SERVER['PHP_SELF'] not activating if(isset($_POST['submit']))


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>
-----------------------------------------------

Jiin
05-14-2010, 09:58 AM
I had:
<input type="submit" value="submit" />

What I needed was:
<input type="submit" name="submit" value="submit" />

The if(isset($_POST['submit'])) section of my code wasn't being triggered because I had not named it i.e. name="submit"

svidgen
05-14-2010, 10:33 AM
Glad you got it figured out.

Jiin
05-14-2010, 03:13 PM
Glad you got it figured out.

Thanks, me too!