English 中文(简体)
$ _POST是空的表单提交后(关闭)
原标题:
  • 时间:2009-05-12 18:38:34
  •  标签:

我建立这个测试页面服务器。请告诉我为什么$ _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>

多年来我一直在编写PHP这从未发生过。这段代码有什么问题?

最佳回答

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

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

如果一个输入元素没有名称属性,它不是作为POST数据的一部分发送。

问题回答

嗯,你不要有形式标记的行动?应该是脚本名称:

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

…你还没有设置每个表单输入的名字——浏览器并提交ID作为元素名称。

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

年代没有输入元素的name属性。

我建议你写类似下功能基于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)

然后你将永远不会忘记/小姐这样的了。

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

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

你忘记这个名字属性使您的脚本工作。你也可以在标签中包含“for”标签来匹配输入名称的属性。不是一个需求,但可以帮助用CSS设计的表单:

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

帮助匹配的一切,让你的代码更简化和可读的如果你有6个月后回来。action属性如果你不去填充一个我将包括这是你的行动:

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

这将确保你的页面很好到表单需求以及做脚本应该执行。似乎是一个简单的高于视觉。希望这个有帮助。





相关问题
热门标签