1

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?

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

You're very welcome. I hope your future php endeavors go well!
3

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.

Comments

1

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>

Comments

1

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.