php Codeigniter $this->input->post() 为空,而 $_POST 工作正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12376251/
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
Codeigniter $this->input->post() empty while $_POST is working correctly
提问by Matthew Higgins
On a codeigniter installation, I am trying to use the inbuilt $this->input->post('some_data')function, however $this->input->post()is an empty array.
在 codeigniter 安装中,我尝试使用内置$this->input->post('some_data')函数,但它$this->input->post()是一个空数组。
A print_r($_POST)gives all the variables fully and correctly?
Aprint_r($_POST)完全正确地给出所有变量?
According to the codeigniter docs, The input class "is initialized automatically by the system so there is no need to do it manually", leaving me wondering what else I'm meant to do.
根据 codeigniter 文档,输入类“由系统自动初始化,因此无需手动执行”,让我想知道我还想做什么。
Any thoughts on how I should try to get this working?
关于我应该如何尝试让它工作的任何想法?
Thanks!
谢谢!
回答by Rauli Rajande
You can check, if your view looks something like this (correct):
您可以检查一下,如果您的视图看起来像这样(正确):
<form method="post" action="/controller/submit/">
vs (doesn't work):
vs(不起作用):
<form method="post" action="/controller/submit">
Second one here is incorrect, because it redirects without carrying over post variables.
这里的第二个是不正确的,因为它重定向而不携带 post 变量。
Explanation:
解释:
When url doesn't have slash in the end, it means that this points to a file.
当 url 末尾没有斜杠时,表示 this 指向一个文件。
Now, when web server looks up the file, it sees, that this is really a directory and sends a redirect to the browser with a slash in the end.
现在,当 Web 服务器查找该文件时,它会发现这实际上是一个目录,并会向浏览器发送重定向,并以斜杠结尾。
Browser makes new query to the new URL with slash, but doesn't post the form contents. That's where the form contents are lost.
浏览器使用斜杠对新 URL 进行新查询,但不发布表单内容。这就是表单内容丢失的地方。
回答by Steward Godwin Jornsen
To use $this->input->post()initialize the form helper. You could do that by default in config.
使用$this->input->post()初始化表单助手。默认情况下,您可以在配置中执行此操作。
回答by Italo Hernández
Try This, change
试试这个,改变
$config['base_url'] = 'http://mywebsite.cl/';
to
到
$config['base_url'] = 'http://www.mywebsite.cl/';
Some provider auto add www , https , etc to your url, most of the times, this causes this issue
某些提供程序会自动将 www 、 https 等添加到您的 url 中,大多数情况下,这会导致此问题
回答by Ajie Winugs
Change your form action,
改变你的表单动作,
From this:
由此:
<form method="post" action="<?=base_url()?>yourprocess">
Into this:
进入这个:
<form method="post" action="<?=base_url()?>index.php/yourprocess">
Basically, you just need to add "index.php" after your base_url():
基本上,您只需要在 base_url() 之后添加“index.php”:
action="http://www.yourdomain.com/index.php/yourprocess"
行动= “http://www.yourdomain.com/的index.php/ yourprocess”
回答by syyu
Upgrading from 2.2.x to 3.0.x -- reason post method fails. If you are upgrading CI 3.x, need to keep the index_page in config file. Also check the .htaccess file for mod_rewrite. CI_3.x
从 2.2.x 升级到 3.0.x - 原因 post 方法失败。如果您要升级 CI 3.x,需要在配置文件中保留 index_page。还要检查 .htaccess 文件中的 mod_rewrite。CI_3.x
$config['index_page'] = ''; // ci 2.x
$config['index_page'] = 'index.php'; // ci 3.x
My .htaccess
我的.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
回答by ShaneH
There's a few things you can look for help solve this.
您可以寻求一些帮助来解决此问题。
Has anything changed or been extended in the core CodeIgniter files. Check that
system/core/Input.phpis an original copy and the contents ofapplication/libraryandapplication/corefor additional filesDo the other input methods work? What is the result of this when run beside your
print_rcall?echo $this->input->user_agent();What data is output from
print_r? Look inapplication/config/config.phpfor the line$config['global_xss_filtering']. Is this set toTRUEorFALSE? IfTRUEmaybe the cross site scripting filter has a problem with the data you're posting (this is unlikely I think)
核心 CodeIgniter 文件中是否有任何更改或扩展。检查
system/core/Input.php是正本和内容application/library以及application/core其他文件其他输入法有用吗?在您的
print_r电话旁边运行时,结果是什么?echo $this->input->user_agent();输出什么数据
print_r?寻找application/config/config.php线路$config['global_xss_filtering']。这是设置为TRUE还是FALSE?如果TRUE跨站点脚本过滤器对您发布的数据有问题(我认为这不太可能)
回答by Webmaster G
My issue was with an ajax call. I was using this:
我的问题是 ajax 调用。我正在使用这个:
type: 'posts',
type: 'posts',
instead of
代替
type: 'post',
type: 'post',
Syntax, D'oh!
语法,天啊!
回答by Jay Bharat
Use
用
var_dump($this->input->post());
Please see the screen shot. Here I am priniting all the post array value
请查看屏幕截图。 在这里,我正在打印所有 post 数组值
回答by user3262234
Finally got the issue resolved today. The issue was with the .htaccessfile.
今天终于把问题解决了。问题出在.htaccess文件上。
Learning to myself: MUST READ THE CODEIGNITER DOCUMENTATIONmore thoroughly.
自我学习:必须更彻底地阅读 CODEIGNITER 文档。
回答by facultymatt
The problem is that $this->input->post()does not support getting all POST data, only specific data, for example $this->input->post('my_post_var').This is why var_dump($this->input->post());is empty.
问题是$this->input->post()不支持获取所有的POST数据,只支持获取特定的数据,例如$this->input->post('my_post_var').This is why var_dump($this->input->post());is empty。
A few solutions, stick to $_POST for retrieving all POST data, or assign variables from each POST item that you need, for example:
一些解决方案,坚持使用 $_POST 来检索所有 POST 数据,或者从您需要的每个 POST 项目中分配变量,例如:
// variables will be false if not post data exists
$var_1 = $this->input->post('my_post_var_1');
$var_2 = $this->input->post('my_post_var_2');
$var_3 = $this->input->post('my_post_var_3');
However, since the above code is not very DRY, I like to do the following:
但是,由于上面的代码不是很 DRY,所以我喜欢做以下事情:
if(!empty($_POST))
{
// get all post data in one nice array
foreach ($_POST as $key => $value)
{
$insert[$key] = $value;
}
}
else
{
// user hasen't submitted anything yet!
}

