-3

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

所以我的第一个表单 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 时它工作正常,然后我输入“用户名”和“电子邮件”。之后,它给出了以下错误:

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

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

4

3 回答 3

3

正如您在php.net上所读到的,$HTTP_POST_VARS已弃用。实际上它们自 PHP5.4 以来不再可用,所以难怪它说Undefined variable.

$username,这仅适用于REGISTER_GLOBALS=ON-您的 WAMP 可能已打开OFF,因为它应该是。

总结总结:使用$_POST

于 2013-03-26T21:46:14.003 回答
2

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

于 2013-03-26T21:47:24.587 回答
0

The function import_request_variables() has been deprecated and removed as of php version 5.4.0.

Use $_POST instead of $HTTP_POST_VARS. The latter is deprecated also.

Do something like this instead:

<?php
$userName = '';
$email = '';

if (isset($_POST['username'])) {
    $userName = $_POST['username'];
}

if (isset($_POST['email'])) {
    $email = $_POST['email'];
}


echo 'username=' . $userName;
echo 'email=' . $email;
?>
于 2013-03-26T21:49:51.923 回答