Jump to content

HTML Form data not showing up in $_POST['myvarhere']


Jiin

Recommended Posts

Hello All, Thanks in Advance!

I am using PHP5 on Linux Apache server side

 

My script is supposed  to:

1) Automatically generate an HTML form from an XML file. (This works)

2) Then,  when the user fills out the HTML form and clicks "submit" ($_SERVER['PHP_SELF'] functionality) the script is supposed to write the HTML form name/values to an XML file as Tag/Values.

 

The first part works. It creates a HTML form from an XML template file I have. I then enter the info and click submit but no data is written. All the $_POST vars are empty!  :confused:

I am at a loss, 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>

-----------------------------------------------

Link to comment
Share on other sites

First, view the HTML source and make sure the form fields are being named as you would expect, then print_r($_POST); to see what the $_POST array actually holds.

 

 

Also, your submit button has no name attribute. <input type="submit" name="submit" value="Submit" />

Link to comment
Share on other sites

Thank you Pikachu2000,

 

This is the result of print_r($_POST):

Array ( [()] => [submit] => submit )

**It looks like $_POST is empty.

 

And on the submit ... my bad, I DO have the name="submit"  in there, I just screwed up pasting into the forum.

<input type="submit" name="submit"  value="Submit" />

 

I am still not sure whats up :-\

 

 

Link to comment
Share on other sites

Did you view the HTML source and are the form fields named properly in it? Without knowing what the variables are supposed to be, and being able to run this code locally, that's about as far as I can take this one . . .

Link to comment
Share on other sites

As far as I can tell they are all named correctly. I am just using tags from xml file to name the input elements of the HTML form. Then I fill out the HTML form, click submit and populate the XML file with data. At least that is the way it is supposed to work.  :confused:

 

Here is the php form:

http://realestatetechnologiesinternational.com/xml/AddNewProperty.php

 

Here is my XML template: http://realestatetechnologiesinternational.com/properties/propertytemplate.xml

Link to comment
Share on other sites

Whaaaaaaaaaaahoooooooo!!! :D

 

You nailed it.

 

My code was generating an empty HTML input name="()":

mls_id: <br /><input type="text" size="60" name="()" value="" />.

 

 

Here is the suspect peice of code:

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 />";

}

 

 

Notice the first call to $child->getName() works but my second call doesn't. To fix it, the first time I call $child->getName I then store it in the $xmltagname variable and then echo that variable.

 

Here is the working version:

foreach($xml->children() as $child) {

        $xmltagname = $child->getName();

        echo $xmltagname . ": " . $child . "<br />";

        echo "<input type=\"text\" size=\"60\" name=\"$xmltagname\" value=\"$child\" />";

        echo "<br /><br /><br />";

}

 

 

Thanks a lot Pikachu2000, you made my day! 8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.