Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php表单标签不工作?(未定义的变量错误)_Php_Html - Fatal编程技术网

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

php表单标签不工作?(未定义的变量错误),php,html,Php,Html,首先,我正在使用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" va

首先,我正在使用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>
<?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;
?>

名称:
电子邮件:
而foo.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>
<?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\u POST\u VARS 注意:未定义变量:第19行C:\wamp\www\foo.php中的用户名

很明显,这些代码应该是有效的,但出于某种原因,它对我不起作用。wamp服务器有问题吗?或者我设置配置的方式可能有问题??谢谢大家!

如您所知,
$HTTP\u POST\u VARS
已被弃用。实际上,自从PHP5.4之后,它们就不再可用了,所以难怪它会说
未定义变量

$username
,这只适用于
REGISTER\u GLOBALS=ON
-您的WAMP可能会像应该的那样打开
关闭它


Summa summarum:Use
$\u POST
$HTTP\u POST\u VARS
未定义,因为已关闭。如果您使用的是PHP 5.4.0,那么它已被删除。

从PHP 5.4.0版开始,函数import\u request\u variables()已被弃用并删除

使用$\u POST而不是$HTTP\u POST\u VARS。后者也不受欢迎

改为这样做:

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

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

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


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


er。。。第19行抛出错误,因为$username未定义…$HTTP\u POST\u VARS已弃用;看看你用的是什么php版本?基本上,正确答案包含在您的
foo.php
中的注释中?它仍然存在于PHP5中。PHP5.4显然删除了它!