php 表单提交后 $_POST 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/854205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:06:25  来源:igfitidea点击:

$_POST is empty after form submission

phphtmlformspost

提问by Mike Stanford

I set up this test page up on my server. Please tell me why the $_POSTarray does not contain anything even when I submit the form. I have tried this in three different browsers and nothing happens.

我在我的服务器上设置了这个测试页面。请告诉我为什么$_POST即使我提交表单,数组也不包含任何内容。我在三种不同的浏览器中尝试过这个,但没有任何反应。

<?php print_r($_POST);?>

<form method="post">

<p><label>Email: </label>
<input type="text" id="login_email" />
</p>

<p><label>Password: </label>
<input type="password" id="login_password" />
</p>

<p><label>Remember Me?: </label>
<input type="checkbox" id="login_remember" />
</p>

<p>
<input type="submit" value="Login" />
</p>

</form>

I have been writing PHP for years and this has never happened before. What is wrong with this code?

我已经写 PHP 多年了,以前从未发生过这种情况。这段代码有什么问题?

回答by Ayman Hourieh

Your input elements do not have name attributes. Should be:

您的输入元素没有名称属性。应该:

<input type="text" id="login_email" name="login_email" />

If an input element does not have a name attribute, it is not sent as part of the POST data.

如果输入元素没有 name 属性,则它不会作为 POST 数据的一部分发送。

回答by BrynJ

Well, you don't have an action for the form tag? It should be the script name:

好吧,您没有表单标签的操作?它应该是脚本名称:

<form method="post" action="scriptname.php">

...and you're also not setting the names for each form input - the browser doesn't submit the ID as the element name.

...而且您也没有为每个表单输入设置名称 - 浏览器不会提交 ID 作为元素名称。

回答by MichaelICE

<form method="POST" action="<?php echo $PHP_SELF; ?>

<p><label>Email: </label>
<input type="text" name="login_email" />
</p>

<p><label>Password: </label>
<input type="password" name="login_password" />
</p>

<p><label>Remember Me?: </label>
<input type="checkbox" name="login_remember" />
</p>

<p>
<input type="submit" value="Login" />
</p>

</form>

回答by cgp

There's no name attribute for the input elements.

input 元素没有 name 属性。

回答by OIS

I suggest you write something like the follow functions based on the Zend_View helpers.

我建议您根据 Zend_View 助手编写类似以下函数的内容。

formText($name, $value = null, array $attribs = null)
formPassword($name, $value = null, array $attribs = null)
formLabel($id, $text, array $attribs = null)
formHidden($name, $value = null, array $attribs = null)
formSubmit($name = null, $text = null, array $attribs = null)
formSelect($name, $selected, array $attribs = null, array $options = null)
formCheckbox($name, $default, array $attribs = null, array $options = null)

Then you will never forget/miss something like this again.

那么你将永远不会再忘记/错过这样的事情。

<form method="POST" action="<?php echo $PHP_SELF; ?>

<p>
<?php
echo formLabel('login_email', 'Email'), ':',
     formText('login_email'); 
?>
</p>

<p>
<?php
echo formLabel('login_password', 'Password'), ':',
     formPassword('login_password'); 
?>
</p>

<p>
<?php
echo formCheckbox('login_remember'), ' ', 
     formLabel('login_remember', 'Remember me');
?>
</p>

<p>
<?php
echo formSubmit(null, 'Login');
?>
</p>
</form>

Tip:

提示:

  • If id not defined in attribs, id is the same as name, except for labels where id is used in the for="$id" attribute and formHidden should not have a default id either.
  • formCheckbox writes a formHidden by same name before itself with the negative value, so you get a return value if the checkbox is not checked as well.
  • formCheckbox options is an array with the values for checked or unchecked.
  • Use a filter with FILTER_VALIDATE_BOOLEAN to read the return value from a checkbox to check if it was marked or not.
  • 如果 id 未在 attribs 中定义,则 id 与 name 相同,除了在 for="$id" 属性中使用 id 的标签,并且 formHidden 也不应具有默认 id。
  • formCheckbox 在其自身之前用负值写入同名的 formHidden,因此如果未选中复选框,您将获得返回值。
  • formCheckbox 选项是一个数组,其中包含选中或未选中的值。
  • 使用带有 FILTER_VALIDATE_BOOLEAN 的过滤器从复选框读取返回值以检查它是否被标记。

回答by stogdilla

You forgot the name attributes for making your script work. You also can include the "for" tag in your labels to match your input's names attributes. This isn't a requirement but can help with CSS formating of your form:

您忘记了使脚本工作的名称属性。您还可以在标签中包含“for”标签以匹配输入的名称属性。这不是必需的,但可以帮助您设置表单的 CSS 格式:

<p>
<label for="login_email">Email: </label>
<input type="text" name="login_email" id="login_email" />
</p>

Helps match everything of and keep your code more streamline and readable if you have to come back to it 6 months later. The action attribute if you aren't going to populate one I would include this as your action:

如果您必须在 6 个月后重新使用它,则有助于匹配所有内容并使您的代码更加精简和可读。如果您不打算填充一个 action 属性,我会将其包含为您的操作:

<form method="POST" action="<?php echo $PHP_SELF; ?>

This will make sure your page is good as far as the form's requirements as well as do as your script should execute. Seems like a simple over-sight. Hope this helps.

这将确保您的页面符合表单的要求以及您的脚本应该执行的情况。似乎是一个简单的疏忽。希望这可以帮助。

回答by Jon Ursenbach

All of your input elements need a name attribute.

所有输入元素都需要一个 name 属性。