PHP POST 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9914979/
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-26 07:55:12  来源:igfitidea点击:

PHP POST not working

phphtmlformspostecho

提问by Wasim A.

<?php echo $_POST['ss'];?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input name="ss" type="text" />
<input type="submit" name="submit">
</form>

This code should print whatever is enter in text box name="ss" when click submit.
But its not printing. Working with method="get" but not with post, What's the problem.

单击提交时,此代码应打印在文本框 name="ss" 中输入的任何内容。
但它不打印。使用 method="get" 但不使用 post,有什么问题。

回答by Matthew Blancarte

If you're just refreshing the page, do:

如果您只是刷新页面,请执行以下操作:

action=''

instead of:

代替:

action="<?php echo $_SERVER['PHP_SELF'];?>"

Also, add this to line 2 to see what's being stored (if anything) in the $_POST array:

此外,将其添加到第 2 行以查看 $_POST 数组中存储的内容(如果有):

var_dump( $_POST );

Hmm... so it's empty on submit? Try adding this to the top of your php file:

嗯...所以提交时它是空的?尝试将其添加到 php 文件的顶部:

if(empty($_SERVER['CONTENT_TYPE']))
{ 
  $_SERVER['CONTENT_TYPE'] = "application/x-www-form-urlencoded"; 
}

Okay, now check your php.ini (normally requires sudo or root in /etc):

好的,现在检查您的 php.ini(通常需要 /etc 中的 sudo 或 root):

post_max_size = 8M
variables_order = "EGPCS"

Do you have those two rules set? If so, be careful of how much memory you're allocating. Anything over 2048MB could start to give you trouble, depending on your system specs.

你有这两个规则吗?如果是这样,请注意您分配了多少内存。任何超过 2048MB 的东西都可能会给您带来麻烦,具体取决于您的系统规格。

NOTE: If you make changes to your php.ini file and PHP is running as an apache module, you'll need to restart apache. Something along the lines of:

注意:如果您更改了 php.ini 文件并且 PHP 作为 apache 模块运行,则需要重新启动 apache。类似的东西:

sudo /etc/init.d/httpd restart

回答by zx1986

I broken my post method once that I set post_max_sizethe same with upload_max_filesize.

一旦我post_max_sizeupload_max_filesize.

I think that post_max_sizemust 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 Nidhin

It may be due to rewrite rules in the .htaccess file.Add this condition to your .htaccess file

这可能是由于 .htaccess 文件中的重写规则。将此条件添加到您的 .htaccess 文件

RewriteCond %{REQUEST_METHOD} !POST [NC]

OR add this line

或添加这一行

 RewriteRule ^welcome_post.php - [PT]

回答by Subham Debnath

Finally ...Got it.... Firstly I have an Articlefolder in my htdocsfile with welcome.phpas the php file in focus and a main HTMLfile , whose contents we will be discussing about. Here's what worked for me.. Turns out the problem was not XAMPP or any of its related files.. The problem was this :

终于...明白了....首先我有一个Article在我的文件夹,htdocs文件,welcome.php作为焦点的PHP文件和主HTML文件,其内容我们将要讨论一下。这对我有用..原来问题不是XAMPP或其任何相关文件..问题是这样的:

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

With this code whenever I pressed the submit button, the url redirects to here file:///C:/xampp/htdocs/Article/welcome.php, Which is not what it needs to be..It has to be a localhost link ..So I changed the value of action attribute to localhost form and now it looks like this

每当我按下提交按钮时,使用此代码,url 重定向到这里file:///C:/xampp/htdocs/Article/welcome.php,这不是它需要的..它必须是一个 localhost 链接..所以我将 action 属性的值更改为 localhost 形式,现在它看起来像这样

<form action="https://localhost/Article/welcome.php" method="post">

That did the trick..Now the submit button redirects to https://localhost/Article/welcome.php, this link..Which is exactly what is needed.. Remember the browser may ask you for some permission issues ..Just accept them it will run fine... Best of luck.. P.S : This one is for Windows..Hope it will work also in Linux and Mac.

这样做的伎俩..现在提交按钮重定向到https://localhost/Article/welcome.php,这个链接..这正是所需要的..记住浏览器可能会问你一些权限问题..只要接受它们它就会运行良好......祝你好运.. PS:这个是针对Windows的..希望它也能在Linux和Mac上运行。

回答by Kranthi Kumar

I solved mine with including following into header.

我通过在标题中包含以下内容解决了我的问题。

Content-Type: application/x-www-form-urlencoded

Just use this in the header while making a request and my problem was solved.

只需在发出请求时在标题中使用它,我的问题就解决了。

回答by mjkaufer

My friend ran into this problem today. The answer was pretty simple - basically, you have to capitalize the POSTpart of method="POST"

我的朋友今天遇到了这个问题。答案是很简单-基本上,你必须大写POST的部分method="POST"

The final result should look like

最终结果应该是这样的

<?php echo $_POST['ss'];?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input name="ss" type="text" />
<input type="submit" name="submit">
</form>

回答by zeeawan

First make sure that your web service (GET/POST etc) is acting as desired using the Chrome Advanced Rest Client. Then you should check your PHP part.

首先使用Chrome Advanced Rest Client确保您的网络服务(GET/POST 等)按预期运行。然后你应该检查你的 PHP 部分。

回答by JWC May

<form action="" method="post">method="post" is important for POST Data.

<form action="" method="post">method="post" 对 POST 数据很重要。

Use PHP REQUEST instead:

使用 PHP REQUEST 代替:

<form action="" method="post">
  <input type="email" name="mail">
  <input type="submit" name="submit" value="Submit">
</form>
PHP:

if(isset($_REQUEST['submit'])){
  $val= $_REQUEST['mail'];
  echo $val;
}

回答by samy

change your IDE ,i use phpstorm ,it's fantastic but when i use dreamweaver it works probably, for test you can run your page directly from wampserver localhost , i change default port of apache and i think problem is from there , if you use phpstorm or change port of apache server change your IDE.

更改您的 IDE,我使用 phpstorm,这太棒了,但是当我使用 Dreamweaver 时它可能可以工作,为了测试,您可以直接从 wampserver localhost 运行您的页面,我更改了 apache 的默认端口,我认为问题出在那里,如果您使用 phpstorm 或更改 apache 服务器的端口更改您的 IDE。

回答by Shema_Thomas

use this instead;

改用这个;

$variable_name = $_REQUEST["ss"];
echo $variable_name;