PHP/HTML 表单提交
我有 HTML 和 PHP
<form name="addaserver" method="post" action="addaserver.php">
<p>Server Name<form method="post">
<input name="servername" type="text" /></form>
<p>Description<form method="post">
<input name="description" type="text" /></form>
<p>Server IP<form method="post">
<input name="ip" type="text" /></form>
<p>Tags (ex: "pvp, economy, fun")<form method="post">
<input name="tags" type="text" /></form>
<form method="post">
<input name="submitserver" type="submit" value="submit" /></form>
</p>
代码 (addaserver.php)
$servername=$_POST['servername'];
$desc=$_POST['description'];
$ip=$_POST['ip'];
$tags=$_POST['tags'];
显然我正在尝试从表单中获取数据...但是,当我单击“提交”时,它只是重新加载表单所在的页面。这可能只是一些简单的错误,但我无法弄清楚出了什么问题 D:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该只定义一种形式,而不是为每个输入定义一种形式:
You're supposed to define only one form, not one for each input:
我看到的第一件事是错误的,即同一个 HTML 中有两个单独的表单标签。
第二个几乎没有用,因为它不向任何目标或操作提供任何数据。我会重组你的 HTML 使其更像这样并尝试一下;
另请注意,我删除了所有结束表单标签,因为它们也会引起问题。您只需要在表单主体的最外侧部分有一个结束标记,如代码示例所示。
First thing I see wrong is that you have two separate form tags in the same HTML.
The second one is pretty much useless as it provides no data to any target or action. I would restructure your HTML to be more like this and try it;
Also take note of the fact that I got rid of all your closing form tags as they would have caused issues too. You only need one closing tag at the very outside most segment of your form's body as shown in the code sample too.
您的代码中有太多
您的代码应以
结尾,但中间应该只有输入字段。
您在第一个
You have way too many
<form method="post">
tags in your code.Your code should start with
<form method="post">
and end with</form>
, but in between there should only be input fields.You define action to 'addaserver.php' in the first
<form>
tag, but the submission button is after a different<form>
tag so it doesn't respect that initial target you are setting.您似乎将所有输入元素都包含在不同的标签中。表单标签是表单元素的集合,提交表单时将提交其值。如果您没有在表单上指定操作属性,它将(如您所说)重新加载页面。因此,在上面的示例中,如果您删除输入标签周围的所有标签并将它们全部放在同一标签下,您应该会发布您的信息
查看 http://www.w3schools.com/html/html_forms.asp 和 http://www.tizag.com/phpT/examples/formex.php 了解如何执行此操作的示例。
希望这是有道理的。
You seem to be enclosing all your input element in different tags. a Form tag is a collection of Form elements that will have their values submitted when the form is submitted. And if you do not specify the action attribute on a form it will (as you say) reload the page. So in the above example if you remove all the tags surrounding the input tags and put them all under the same tag you should get your information posted
Look at http://www.w3schools.com/html/html_forms.asp and http://www.tizag.com/phpT/examples/formex.php for examples on how to do this.
Hope that makes sense.
您只需要一个表单标签即可提交整个表单
You only need one form tag for the whole form to submit
您在整个表单中嵌套了额外的表单标签。您只需要一个表单标签。所有输入都在其中。
You're nesting extra form tags throughout your form. You only need one form tag. All of the inputs go inside it.
试试这个:
Try this instead: