PHP $_POST 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9002424/
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
PHP $_POST not working?
提问by ptinca
I have the simplest form possible and all I want to do is echo whatever is written in text box.
我有最简单的形式,我想做的就是回显文本框中写的任何内容。
HTML:
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
PHP:
if(isset($_POST['submit'])){
$test = $_POST['firstname'];
echo $test;
}
The problem is it's not working on my server (it works on another server). Does anyone has an idea what could be wrong? There are other forms on the server and are working fine.
问题是它在我的服务器上不起作用(它在另一台服务器上起作用)。有谁知道可能有什么问题?服务器上还有其他表单并且工作正常。
回答by Frankline
A few thing you could do:
你可以做的几件事:
- Make sure that the "action" attribute on your form leads to the correct destination.
- Try using $_REQUEST[] instead of $_POST, see if there is any change.
[Optional] Try including both a 'name' and an 'id' attribute e.g.
<input type="text" name="firstname" id="firstname">
If you are in a Linux environment, check that you have both Read/Write permissions to the file.
- 确保表单上的“操作”属性指向正确的目的地。
- 尝试使用 $_REQUEST[] 而不是 $_POST,看看是否有任何变化。
[可选] 尝试同时包含 'name' 和 'id' 属性,例如
<input type="text" name="firstname" id="firstname">
如果您在 Linux 环境中,请检查您是否拥有该文件的读/写权限。
In addition, this linkmight also help.
此外,此链接也可能有所帮助。
EDIT:
编辑:
You could also substitute
你也可以代替
<code>if(isset($_POST['submit'])){</code>
with this:
有了这个:
<code>if($_SERVER['REQUEST_METHOD'] == "POST"){ </code>
This is always the best way of checking whether or not a form has been submitted
这始终是检查表单是否已提交的最佳方式
回答by geoidesic
I had something similar this evening which was driving me batty. Submitting a form was giving me the values in $_REQUEST but not in $_POST.
今晚我遇到了类似的事情,这让我很生气。提交表单给了我 $_REQUEST 中的值,而不是 $_POST 中的值。
Eventually I noticed that there were actually two requests going through on the network tab in firebug; first a POST with a 301 response, then a GET with a 200 response.
最终我注意到在 firebug 的网络选项卡上实际上有两个请求正在通过;首先是带有 301 响应的 POST,然后是带有 200 响应的 GET。
Hunting about the interwebs it sounded like most people thought this was to do with mod_rewrite causing the POST request to redirect and thus change to a GET.
搜索互联网,听起来大多数人认为这与 mod_rewrite 导致 POST 请求重定向并因此更改为 GET 有关。
In my case it wasn't mod_rewrite to blame, it was something far simpler... my URL for the POST also contained a GET query string which was starting without a trailing slash on the URL. It was this causing apache to redirect.
在我的情况下,这不是 mod_rewrite 的罪魁祸首,它要简单得多......我的 POST 的 URL 还包含一个 GET 查询字符串,该字符串在 URL 上没有尾部斜杠。正是这导致 apache 重定向。
Spot the difference...
指出不同...
Bad: http://blah.de.blah/my/path?key=value&otherkey=othervalue
Good: http://blah.de.blah/my/path/?key=value&otherkey=othervalue
坏: http://blah.de.blah/my/path?key=value&otherkey=othervalue
好: http://blah.de.blah/my/path/?key=value&otherkey=othervalue
The bottom one doesn't cause a redirect and gives me $_POST!
底部不会导致重定向并给我 $_POST!
回答by thefid
Dump the global variable to find out what you have in the page scope:
转储全局变量以找出页面范围内的内容:
var_dump($GLOBALS);
This will tell you the "what" and "where" regarding the data on your page.
这将告诉您有关页面上数据的“内容”和“位置”。
回答by Angel Aparicio
I also had this problem. The error was in the htaccess. If you have a rewrite rule that affects the action url, you will not able to read the POST variable.
我也有这个问题。错误出在 htaccess 中。如果您有影响操作 url 的重写规则,您将无法读取 POST 变量。
To fix this adding, you have to add this rule to htaccess, at the beginning, to avoid to rewrite the url:
要修复这个添加,你必须在开始时将此规则添加到htaccess,以避免重写url:
RewriteRule ^my_action.php - [PT]
重写规则 ^my_action.php - [PT]
回答by Deniz Cakiroglu
Instead of using $_POST, use $_REQUEST:
使用 $_REQUEST 代替 $_POST:
HTML:
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
PHP:
if(isset($_REQUEST['submit'])){
$test = $_REQUEST['firstname'];
echo $test;
}
回答by zx1986
Have you check your php.ini ?
I broken my post method once that I set post_max_size
the same with upload_max_filesize
.
你检查过你的 php.ini 了吗?
一旦我post_max_size
用upload_max_filesize
.
I think that post_max_size
must less than upload_max_filesize
.
Tested with PHP 5.3.3 in RHEL 6.0
我想那post_max_size
一定小于upload_max_filesize
。
在 RHEL 6.0 中用 PHP 5.3.3 测试
回答by Mugunthan S
try this
html code
试试这个
html 代码
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
php code:
php代码:
if(isset($_POST['Submit'])){
$firstname=isset($_POST['firstname'])?$_post['firstname']:"";
echo $firstname;
}
回答by PiTheNumber
There is nothing wrong with your code. The problem is not visible form here.
您的代码没有任何问题。问题在这里不可见。
Check if after the submit, the script is called at all.
Have a look at what is submitted:
var_dump($_REQUEST)
检查提交后是否完全调用脚本。
看看提交的内容:
var_dump($_REQUEST)
回答by max
try doing var_dump($_GLOBALS)
.
尝试做var_dump($_GLOBALS)
。
A potential cause could be that there is a script running before yours which unsets the global variables. Such as:
一个潜在的原因可能是在您之前运行的脚本会取消设置全局变量。如:
unset($_REQUEST);
or even.
甚至。
unset($GLOBALS);
This could be done via the auto_prepend_file option in the php.ini configuration.
这可以通过 php.ini 配置中的 auto_prepend_file 选项来完成。
回答by AdityaSharma Malladi
- html file and php file both should reside in htdocs folder in c:\apache2 (if you use apache web server).
- Open html file by typing http://"localhost/html_file_name.html"
- Now enter Your data in fields.. Your code will run..
- .html 文件和 php 文件都应位于 c:\apache2 的 htdocs 文件夹中(如果您使用 apache 网络服务器)。
- 通过键入 http://"localhost/html_file_name.html" 打开 html 文件
- 现在在字段中输入您的数据..您的代码将运行..