Codeigniter $ this-> input-> post()为空,而$ _POST正常工作

时间:2022-10-06 16:37:51

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?

print_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!

谢谢!

13 个解决方案

#1


23  

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最后没有斜杠时,这意味着它指向一个文件。

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进行新查询,但不发布表单内容。这就是表单内容丢失的地方。

#2


4  

To use $this->input->post() initialize the form helper. You could do that by default in config.

要使用$ this-> input-> post()初始化表单助手。您可以在配置中默认执行此操作。

#3


1  

Have you enabled the csrf protection in /applicaiton/config/config.php ?

您是否在/applicaiton/config/config.php中启用了csrf保护?

If so $this->input->post() will appear empty. You might try doing $this->input->post('submit') which should not be empty.

如果是这样,$ this-> input-> post()将显示为空。您可以尝试使用$ this-> input-> post('submit'),它不应为空。

#4


1  

There's a few things you can look for help solve this.

你可以寻找一些帮助来解决这个问题。

  1. Has anything changed or been extended in the core CodeIgniter files. Check that system/core/Input.php is an original copy and the contents of application/library and application/core for additional files

    在CoreIgniter核心文件中有任何更改或扩展。检查system / core / Input.php是否为原始副本,以及其他文件的application / library和application / core的内容

  2. Do the other input methods work? What is the result of this when run beside your print_r call?

    其他输入法是否有效?在print_r呼叫旁边运行时,结果是什么?

    echo $this->input->user_agent();

    echo $ this-> input-> user_agent();

  3. What data is output from print_r? Look in application/config/config.php for the line $config['global_xss_filtering']. Is this set to TRUE or FALSE? If TRUE maybe the cross site scripting filter has a problem with the data you're posting (this is unlikely I think)

    print_r输出什么数据?在application / config / config.php中查找$ config ['global_xss_filtering']行。这设置为TRUE还是FALSE?如果为TRUE,则跨站点脚本过滤器可能会对您发布的数据有疑问(我认为这不太可能)

#5


1  

My issue was with an ajax call. I was using this:

我的问题是ajax调用。我正在使用这个:

type: 'posts',

类型:'帖子',

instead of

代替

type: 'post',

类型:'post',

Syntax, D'oh!

语法,D'哦!

#6


1  

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>

#7


1  

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等添加到您的网址,大多数情况下,这会导致此问题

#8


0  

Use

使用

var_dump($this->input->post());

Please see the screen shot. Here I am priniting all the post array value

请看屏幕截图。这里我打印所有post数组值

#9


0  

Finally got the issue resolved today. The issue was with the .htaccess file.

终于在今天解决了这个问题。问题出在.htaccess文件中。

Learning to myself: MUST READ THE CODEIGNITER DOCUMENTATION more thoroughly.

向自己学习:必须更彻底地阅读CODEIGNITER DOCUMENTATION。

#10


0  

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”

#11


-2  

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')。这就是var_dump($ this-> input-> post());是空的。

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:

但是,由于上面的代码不是很干,我喜欢这样做:

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!
}

#12


-2  

Solved as follows:

解决如下:

Does the following exclude the redirection that is .htacess that will run the $ this-> input- > post ('name' ) on the controller, in my case it worked perfectly .

以下是否排除了将在控制器上运行$ this-> input-> post('name')的.htacess重定向,在我的情况下它完美地运行。

abs, José Camargo]

绝对,何塞卡马戈]

#13


-3  

To see all the post value try var_dump($this->input->post(ALL));

要查看所有post值,请尝试var_dump($ this-> input-> post(ALL));

#1


23  

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最后没有斜杠时,这意味着它指向一个文件。

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进行新查询,但不发布表单内容。这就是表单内容丢失的地方。

#2


4  

To use $this->input->post() initialize the form helper. You could do that by default in config.

要使用$ this-> input-> post()初始化表单助手。您可以在配置中默认执行此操作。

#3


1  

Have you enabled the csrf protection in /applicaiton/config/config.php ?

您是否在/applicaiton/config/config.php中启用了csrf保护?

If so $this->input->post() will appear empty. You might try doing $this->input->post('submit') which should not be empty.

如果是这样,$ this-> input-> post()将显示为空。您可以尝试使用$ this-> input-> post('submit'),它不应为空。

#4


1  

There's a few things you can look for help solve this.

你可以寻找一些帮助来解决这个问题。

  1. Has anything changed or been extended in the core CodeIgniter files. Check that system/core/Input.php is an original copy and the contents of application/library and application/core for additional files

    在CoreIgniter核心文件中有任何更改或扩展。检查system / core / Input.php是否为原始副本,以及其他文件的application / library和application / core的内容

  2. Do the other input methods work? What is the result of this when run beside your print_r call?

    其他输入法是否有效?在print_r呼叫旁边运行时,结果是什么?

    echo $this->input->user_agent();

    echo $ this-> input-> user_agent();

  3. What data is output from print_r? Look in application/config/config.php for the line $config['global_xss_filtering']. Is this set to TRUE or FALSE? If TRUE maybe the cross site scripting filter has a problem with the data you're posting (this is unlikely I think)

    print_r输出什么数据?在application / config / config.php中查找$ config ['global_xss_filtering']行。这设置为TRUE还是FALSE?如果为TRUE,则跨站点脚本过滤器可能会对您发布的数据有疑问(我认为这不太可能)

#5


1  

My issue was with an ajax call. I was using this:

我的问题是ajax调用。我正在使用这个:

type: 'posts',

类型:'帖子',

instead of

代替

type: 'post',

类型:'post',

Syntax, D'oh!

语法,D'哦!

#6


1  

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>

#7


1  

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等添加到您的网址,大多数情况下,这会导致此问题

#8


0  

Use

使用

var_dump($this->input->post());

Please see the screen shot. Here I am priniting all the post array value

请看屏幕截图。这里我打印所有post数组值

#9


0  

Finally got the issue resolved today. The issue was with the .htaccess file.

终于在今天解决了这个问题。问题出在.htaccess文件中。

Learning to myself: MUST READ THE CODEIGNITER DOCUMENTATION more thoroughly.

向自己学习:必须更彻底地阅读CODEIGNITER DOCUMENTATION。

#10


0  

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”

#11


-2  

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')。这就是var_dump($ this-> input-> post());是空的。

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:

但是,由于上面的代码不是很干,我喜欢这样做:

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!
}

#12


-2  

Solved as follows:

解决如下:

Does the following exclude the redirection that is .htacess that will run the $ this-> input- > post ('name' ) on the controller, in my case it worked perfectly .

以下是否排除了将在控制器上运行$ this-> input-> post('name')的.htacess重定向,在我的情况下它完美地运行。

abs, José Camargo]

绝对,何塞卡马戈]

#13


-3  

To see all the post value try var_dump($this->input->post(ALL));

要查看所有post值,请尝试var_dump($ this-> input-> post(ALL));