php 表单提交后 $_POST 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15109603/
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
$_POST is empty after form submit
提问by pangi
I am using Twitter Bootstrap to build a web application. I have a form where user should enter his name and last name and click submit. The problem is, that the $_POSTcomes back empty.
I am using WAMP on Windows 8 machine.
我正在使用 Twitter Bootstrap 构建一个 Web 应用程序。我有一个表单,用户应该在其中输入他的姓名和姓氏,然后单击提交。问题是,$_POST返回的是空的。我在 Windows 8 机器上使用 WAMP。
<?php
if(isset($_POST["send"])) {
print_r($_POST)
} else {
?>
<form class="form-horizontal" action="" method="post">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<button type="submit" name="send" class="btn btn-primary">Submit data</button>
<a href="<?php $mywebpage->goback(); ?>" class="btn">Cancel</a>
</div>
</form>
<?php
}
?>
Thanks in advance
提前致谢
回答by Sid
Plz try and add names to the input types like
请尝试将名称添加到输入类型中,例如
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
Now check the $_POST variable.
现在检查 $_POST 变量。
回答by RandomCoder
Your inputs don't have nameattributes. Set those:
您的输入没有name属性。设置那些:
<input type="text" id="inputName" placeholder="First name" name="first_name" />
<input type="text" id="inputLastname" placeholder="Last name" name="last_name" >
Also, look at the way you detect a form submission:
另外,看看你检测表单提交的方式:
if(isset($_POST['send']))
You check if the submit button is present in the post data. This is a bad idea because in the case of Internet Explorer, the submit button won't be present in the post data if the user presses the enter key to submit the form.
您检查帖子数据中是否存在提交按钮。这是一个坏主意,因为在 Internet Explorer 的情况下,如果用户按 Enter 键提交表单,则提交按钮将不会出现在发布数据中。
A better method is:
更好的方法是:
if($_SERVER['REQUEST_METHOD'] == 'POST')
Or
或者
if(isset($_POST['first_name'], $_POST['last_name']))
More info - Why isset($_POST['submit'])is bad.
回答by Dan
The problem is that you're checking if $_POST['send']is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST.
问题是您正在检查是否$_POST['send']已设置,但不会。表单中的“发送”字段是按钮,但按钮没有值,因此不会包含在$_POST.
Hope that helps.
希望有帮助。
回答by Nirav Ranpara
<?php
if(isset($_POST["send"])) {
echo $_POST["inputName"];
echo $_POST["inputLastname"];
}
else {
?>
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="control-group"> <!-- Firstname -->
<label class="control-label" for="inputEmail">First name</label>
<div class="controls">
<input type="text" id="inputName" placeholder="First name" name="inputName">
</div>
</div>
<div class="control-group"> <!-- Lastname -->
<label class="control-label" for="inputEmail">last name</label>
<div class="controls">
<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">
</div>
</div>
<div class="form-actions">
<input type="submit" name="send" class="btn btn-primary">Submit data</button>
<a href="#" class="btn">Cancel</a>
</div>
</form>
<?php
}
?>
回答by ErcanE
I have notice that using 'name' attribute doesn't work well with Bootstrap Validatorfor (if(isset($_POST["send"]))).So in case if you like to add validation on your form then I recommend you to follow this way!
我注意到使用 ' name' 属性不适用于Bootstrap Validatorfor (if(isset($_POST["send"]))。因此,如果您想在表单上添加验证,那么我建议您跟着这条路走!
Solution
解决方案
<button type="submit" class="btn btn-primary">Submit data</button>
php
php
if($_SERVER['REQUEST_METHOD'] == 'POST')
回答by Vineet1982
Add the location of the file in action or use:
在操作中添加文件的位置或使用:
<form class="form-horizontal" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

