简体   繁体   English

使用$ _POST变量的PHP if语句似乎不起作用。 为什么?

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

On one PHP server I have two files. 在一台PHP服务器上,我有两个文件。 One file (the name is "first.php") contains this code: 一个文件(名称为“ first.php”)包含以下代码:

<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: 另一个文件(“ pass.php”)包含以下代码:

<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. 在第二页上,而不是检查$ fname,而是检查$ _POST ['fname']。 I think that is all you are missing. 我认为这就是您所缺少的。

You probably don't have register_globals set. 您可能没有设置register_globals。 This is depreciated and will be removed in 6.x. 它已贬值,将在6.x中删除。 So for good programming you should instead of $fname try $_POST['fname'] on your second page. 因此,为了获得良好的编程效果,您应该在第二页上尝试使用$ _POST ['fname']代替$ fname。

It might help to set the post values as variables and work with that. 将post值设置为变量可能会有所帮助。 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. 这样,发布的内容将可用,而不是在您的逻辑中使用$_POST['xxxxx']

pass.php needs to look like this pass.php需要看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM