views:

144

answers:

4

On one PHP server I have two files. One file (the name is "first.php") contains this code:

<html>
<head>
<title>First Page</title>
</head>
<body>
Please enter your password and age:
<form action="pass.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

The other file ("pass.php") contains this code:

<html>
<head>
<title>Secon Page</title>
</head>
<body>
<?php
if ($fname=="Jack")
  echo "You are Jack!";
else
  echo "You are not Jack!";
?>
</body>
</html>

As far as I understand, if a user enters "Jack" in the first page, than the second page should be displayed with "You are Jack!" line, but it doesn't happen. Why is it so?

+3  A: 

You probably don't have register_globals set. This is depreciated and will be removed in 6.x. So for good programming you should instead of $fname try $_POST['fname'] on your second page.

Pete
+5  A: 

On your second page, instead of checking for $fname, check for $_POST['fname'] instead. I think that is all you are missing.

darthnosaj
Thank you!!! It works now!!!
brilliant
You're very welcome. I hope your future php endeavors go well!
darthnosaj
+1  A: 

It might help to set the post values as variables and work with that. Something like this:

foreach($_POST as $key => $value) { $$key = $value; }

Then whatever is posted will be available rather than using $_POST['xxxxx'] in your logic.

chris
This is an even worse idea than register_globals, as it can overwrite any variables you have used in your script previously. And you'll still get loads of warnings for variables that aren't passed in.
bobince
Yes, it can overwrite variables previously set, but you can do that anywhere. Naming is one of the most difficult aspects of programming! However, I would rather not be repeating myself with $_POST['key'] throughout my logic.
chris
+1  A: 

pass.php needs to look like this

<html>
<head>
<title>Secon Page</title>
</head>
<body>
<?php
if ($_POST['fname'] =="Jack")
  echo "You are Jack!";
else
  echo "You are not Jack!";
?>
</body>
</html>
Ben Shelock
Thank you very much.
brilliant