开发者

PHP if-statement using $_POST variable doesn't seem to work. Why?

开发者 https://www.devze.com 2022-12-12 18:00 出处:网络
On one PHP server I have two files. One file (the name is \"first.php\") contains this code: <html>

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") cont开发者_StackOverflow中文版ains 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?


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


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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号