Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another Undefined Variable problem 1

Status
Not open for further replies.

petermeachem

Programmer
Joined
Aug 26, 2000
Messages
2,270
Location
GB
Sorry, I've tried but I don't see what I am supposed to do here. I;ve been playing around with some code I found ages ago in an online tutorial. I get Undefined Variable on the
if ($submit) { line. I've read through most of the other undefined variable posts and I still don't understand. I can see why the error occurs, because it is defined further down. Can this be made to work, or am I barking up the wrong online tutorial?

<?php

if ($submit) {

// process form
while (list($name, $value) = each($HTTP_POST_VARS)) {
echo "$name = $value<br>\n";
}
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php

 
this depends on if you have register_globals turned on in your php.ini file. You probably have it turned off. can you do a php_info() to see if it is turned on? It should be turned off for security reasons.

Try $_POST['submit'] instead
 
or $HTTP_POST_VARS['submit'] for older php versions
 
dont know if this will make a difference
<form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?">
 
Whether you use $submit or $_POST['submit'], in order to avoid that error, you should check for the presence of the variable rather than just referencing it in the if-clause. The first time you run the script, $_POST['submit'] does not exist -- a bare reference to the variable in your if-clause will set off that error.

Replace:
if ($submit)

with:

if (isset($_POST['submit'])
or
if (array_key_exists('submit',$_POST))

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yes, it did. I don't know at what version of PHP the behavior of "if ($foo)" changed.

But the behavior of the reference seems to wander back and forth. "if(isset($_POST['submit'])" works on my PHP 4.3.4 installation.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top