Have working "POST" code (when in separate files)...but not when in one file

Background info: just started learning PHP this weekend; the only language I have studied is C# and am somewhat comfortable with it. My OS is Win7. I downloaded XAMPP and am following an online tutorial and things are going well. Until I ran into the following that I’ve turned upside down and inside out and don’t understand why it won’t work.

Here’s the problem: I have a simple form (“upload”) to upload a file; when the “action” references a different php file (“upload2”) all works as intended. But the moment I take the same code, put it into a single file (“upload3”) the whole thing acts like it’s not posting. The code below are the 3 files referenced here; and the last piece of code is code I wrote from the tutorial, it works as intended, and is the process I attempted to duplicate (so frustrating to write something that works, then something that doesn’t…and you can’t tell the difference between the two; so sad). Thanks in advance for your advice and help.

This is the simple form, “upload”:
[php]

[/php]

This is the server-side response, super trimmed down for testing, “upload2”:
[php]

<?php echo "The world is round." ?>

[/php]

And those work as one would imagine. But this one, “upload3” doesn’t:
[php]

<?php if (!$_POST) { ?> <?php } else { echo "The world is round."; } ?> [/php]

If I change upload3’s action to reference upload2.php everything works fine. It’s as if I can’t get the file to recognize itself. What confounds me, and kept me going on the problem for a couple of hours is previously in the tutorial I followed this method and it worked…and no amount of reckoning by me can figure out the difference:

[php]

PHP Forms <?php if(!$_POST) { $filename=$_SERVER["PHP_SELF"];
	echo '<form method="post" action="'.$filename.'">
	    <p><label for="firstname">Type your first name here: </label><input type="text" name="firstname" id="firstname" /></p>
	    <p><label for="surname">Type your surname here: </label><input type="text" name="surname" id="surname" /></p>
	    <p><label for="dob">Your date of birth (dd/mm/yyyy): </label><input type="text" name="dob" id="dob" /></p>
	<input type="submit" name="submit">
	</form>';

	} else {

	$firstname=$_POST["firstname"];
	$surname=$_POST["surname"];
	$dob=$_POST["dob"];

	echo '<div id="green">';
	echo "Hello $firstname $surname I believe you were born $dob";
	echo '</div>';

	echo '<div style="background-color: yellow;">';
	echo '<p>Using $_POST: </p>';
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
	echo '</div>';
	}
?>       


</body>
[/php]

Hopefully that wasn’t too much information. What am I missing? I look to be following the working model I created early in the tutorial but…yeah… Once again, tremendous thank you to anyone that responds.

Just to be sure, what happens if you don’t test on the entire $_POST but test something like:
[php]
if (! array_key_exists( ‘file01’, $_POST ) )
{

}
[/php]

O.

Thank you for responding, Ojoshiro. The “array_key_exists” is new to me so I fiddled with it until I understood it. The short answer to your inquiry: when using your IF statement the form loads, ie, I think that’s telling us there’s no “file01” key in the $_POST associative array? Which, what, tells us the $_POST array is empty? …and thus no post has occurred?

In an attempt to understand what is happening I plugged the code below into the model I have working (working code: “upload.php” action points to “upload2.php”, where “upload2.php” IF statement either shows a form or the results of POST):

Added code to “upload2.php”:
[php]
echo “

”;
print_r($_POST);
echo “
”;
[/php]

…and when the form (“upload.php”) is submitted, the form disappears and the results of “upload2.php” appear as expected. HOWEVER, I’m confused why the print_r($_POST) depicted an empty array…because something was sent via POST (right?) as that’s the only way “upload2.php” would have been able to take action on “upload.php”?

AND, why that confuses me is because in the following code print_r($_POST) worked just like I thought it would:
A simple form:
[php]

PHP Forms

Type your first name here:

Type your surname here:

Your date of birth (dd/mm/yyyy):

[/php]

And it’s simple processing partner:
[php]

#green{background-color:#00FF00;} PHP Forms <?php $firstname=$_POST["firstname"]; $surname=$_POST["surname"]; $dob=$_POST["dob"];
echo '<div id="green">';
echo "Hello $firstname $surname I believe you were born $dob";
echo '</div>';

echo '<div style="background-color: yellow;">';
echo '<p>Using $_POST: </p>';
echo '<pre>';
print_r($_POST);
echo '</pre>';
echo '</div>';
    ?>        
</body>
[/php]

…and with that code model the print_r($_POST) shows the $_POST array has data in it.

The Greeks had a term, “gnosis”, which means “to know” (I think); when prefixed with “a-” for “agnosia” it means to “not know”. In college we learned one of the worst forms of disease is agnosagnosia, which is basically: I don’t know what I don’t know. There’s something about POST that I’m not grasping and I’m sorry that I can’t articulate it.

So very much extra thank you for your assistance!
-R

This works expected:
[php]

<?php if(!$_POST) { echo ' '; } else { echo '
';
     print_r($_POST);
     echo '
'; } ?>

[/php]

But, Ojoshiro, this one doesn’t:
[php]

<?php if(! array_key_exists ('file01', $_POST)) { echo ' '; } else { echo '
';
     print_r($_POST);
     echo '
'; } ?>

[/php]
…the form shows up, clicking submit causes the selected file to disappear, but the ELSE doesn’t run.

However, this one does work:
[php]

<?php if(! array_key_exists ('submit', $_POST)) { echo ' '; } else { echo '
';
     print_r($_POST);
     echo '
'; } ?>

[/php]
…which I take to mean the “file01” doesn’t make it into the super global “$_POST”. It must go directly to super global “$_FILES”? …hold on, I’ll test that right quick…
[php]

<?php if(! array_key_exists ('file01', $_FILES)) { echo ' '; } else { echo '
';
     print_r($_POST);
     echo '
'; echo '
';
     print_r($_FILES);
     echo '
'; } ?>

[/php]

Okay, that seems to be the issue. This has been time well spent. Thanks once again to Ojoshiro for lending a hand.
PS: on looking over the code that didn’t work (“upload3”) it appears the submit is missing a name-property, which doesn’t seem to make a difference if a separate file processes it, but does make a difference if the processing is within the same file (?that’s a guess, but it does work when the name-property is added).

Sponsor our Newsletter | Privacy Policy | Terms of Service