Many of the solutions I have seen that involve posting to the same page suggest testing for the existence of $submit(Obviously, if register_globals = OFF, we know that we need to check $_POST['submit']. This post is not about that).
However, I submit (pardon the pun) that this is not a good solution. I have on numerous occasions stated that upon posting to itself, a PHP page should test for the existence of a known field, perhaps even a hidden field named "submit" with a value of "yes." Subsequently, I have been told that if one simply uses the name attribute of a submit button:
<input type="submit" value="Submit" name="submit">
... that will solve the problem. Then the page can check for the existence of the variable "submit," right? Absolutely!
So, what's the problem? Get to the point, you say? Fine... try this. First, enter the following code, and save it as submittest.php on your server:
<html>
<head>
<title>Title Here</title>
</head>
<body>
<?
$name = $_POST['name'];
if (isset($_POST['submit'])) {
echo ("Hello $name, how are you today? <br><br>");
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<table border="0" cellpadding="0" cellspacing="3" summary="">
<tr>
<td align="left">What is your name?</td>
<td><input type="text" name="name" /></td>
</tr>
<tr><td colspan="2" align="center">
<input type="submit" value="submit" name="submit" /></td></tr>
</table>
</form>
</body>
</html>
Now, load the page in your browser, and type in your name. Hit submit. Works, doesn't it? Enter a different name (or your name again -- doesn't matter). Hit the ENTER key, instead of hitting submit.
Aha! The problem is... the submit button is only passed to the next form if it is pressed. If the user hits the Enter key to submit the form (which many of us do), the submit variable will never be passed!
To resolve this, use the hidden input, as I have suggested before:
<input type="hidden" name="submit" value="yes">
If you do this, $submit will always make it to the posted form, whether you use the submit button or the Enter key.