PHP表单标签不工作?(未定义变量错误)


php form tags not working? (undefined variable error)

首先,我正在使用wamp服务器并学习基本的PHP语法。

所以我的第一个form PHP文件是
<form action="foo.php" method="post">
Name:  <input type="text" name="username" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>

和foo.php文件是

<?php 
// Available since PHP 4.1.0
echo $_POST['username'];
echo $_REQUEST['username'];
import_request_variables('p', 'p_');
echo $p_username;
// As of PHP 5.0.0, these long predefined variables can be
// disabled with the register_long_arrays directive.
echo $HTTP_POST_VARS['username'];
// Available if the PHP directive register_globals = on. As of 
// PHP 4.2.0 the default value of register_globals = off.
// Using/relying on this method is not preferred.
echo $username;
?>

但是当我打开localhost/form2.php它工作得很好,然后我输入"用户名"answers"电子邮件"。然后给出以下错误:

注意:未定义变量:HTTP_POST_VARS在C:'wamp'www'foo.php第13行注意:未定义变量:用户名在C:'wamp'www'foo.php第19行

显然,这些代码应该是工作的,但由于某种原因,它对我不起作用。wamp服务器有什么问题吗?或者可能是我如何设置配置的问题??

您可以在php.net上看到,$HTTP_POST_VARS已被弃用。事实上,从PHP5.4开始,它们就不再可用了,所以难怪它说Undefined variable

$username,这只适用于REGISTER_GLOBALS=ON -你的WAMP可能有OFF,因为它应该是。

总结:使用$_POST

$HTTP_POST_VARS未定义,因为register_long_arrays已关闭。如果您使用的是PHP 5.4.0,那么已被删除

函数import_request_variables()已被弃用并从php 5.4.0版本删除。

使用$_POST代替$HTTP_POST_VARS。后者也不推荐使用。

不如这样做:

<?php
$userName = '';
$email = '';
if (isset($_POST['username'])) {
    $userName = $_POST['username'];
}
if (isset($_POST['email'])) {
    $email = $_POST['email'];
}

echo 'username=' . $userName;
echo 'email=' . $email;
?>