php Codeigniter $this->input->post 总是 FALSE

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

Codeigniter $this->input->post is always FALSE

phpcodeigniterpost

提问by Mj1992

I am trying to send an httpRequest to a codeigniter controller function. I am using the REST console to test the function . I am trying to send 3 POSTvariables .

我正在尝试将 httpRequest 发送到 codeigniter 控制器功能。我正在使用 REST 控制台来测试该功能。我正在尝试发送3 POST变量。

  • UserName
  • Email
  • UserID
  • 用户名
  • 电子邮件
  • 用户身份

Here's the code to handle the request

这是处理请求的代码

public function NewUser()
{
    if($this->input->post())
    {
        $FID        = $this->input->post('UserID');
        $UserName   = $this->input->post('UserName');
        $Email      = $this->input->post('Email');
        echo "working";
        echo $FID;
        echo $UserName;
    }
    else
    {
        echo "not working";
    }
}

But this doesn't work. It always output's not working. When I change everything to geteverything starts working fine.

但这不起作用。它总是输出not working. 当我将一切更改为get一切时,一切都开始正常工作。

What could be the issue ? Post Request is not working anywhere throughout this codeigniter project.

可能是什么问题 ?Post Request 在此期间的任何地方都不起作用codeigniter project

EDIT

编辑

I created a new script, with the following code.

我使用以下代码创建了一个新脚本。

<?php

  var_dump($_POST);
  echo $_POST['UserName'];
  echo $_POST['FacebookID'];
  echo $_POST['Email'];
  echo "********************************";
?>

It is saying undefined index. What could be the issue ? Please help. It works fine for $_GET

它在说undefined index。可能是什么问题 ?请帮忙。它适用于$_GET

回答by Harshal Mahajan

$this->input->post()is obliviously return the false because you are not mentioning the name of which value you want to retrieve using post.Make changes here in your code :

$this->input->post()不经意地返回 false,因为您没有提到要使用 post.Make 在代码中检索的值的名称:

if(isset($_POST))

or

或者

  if(!empty($_POST))

See POST

POST

you can also do this:

你也可以这样做:

if($this->input->post('username'))//username is the name of post variable

回答by Himanshu Pandey

you should try

你应该试试

isset($_REQUEST)or !empty($_REQUEST)

isset($_REQUEST)或者 !empty($_REQUEST)

to check data is coming or not

检查数据是否到来

回答by MJ Pakzad

To get the method in codeigniter 3 (docs) you can use the following code:

要获取 codeigniter 3 ( docs) 中的方法,您可以使用以下代码:

echo $this->input->method(TRUE); // Outputs: POST
echo $this->input->method(FALSE); // Outputs: post
echo $this->input->method(); // Outputs: post

example:

例子:

public function NewUser()
{
    if($this->input->method() === 'post')
    {
        $FID        = $this->input->post('UserID');
        $UserName   = $this->input->post('UserName');
        $Email      = $this->input->post('Email');
        echo "working";
        echo $FID;
        echo $UserName;
    }
    else
    {
        echo "not working";
    }
}

回答by Christian Giupponi

Try

尝试

if( count($this->input->post()) > 0 )
{

}
else
{

}

回答by Kobus Myburgh

I had a similar problem. Due to using internationalization, URLs get redirected from user/login to user/en/login. When that redirect happens, the POST array gets lost.

我有一个类似的问题。由于使用国际化,URL 会从 user/login 重定向到 user/en/login。当该重定向发生时,POST 数组将丢失。

I am not sure if this is your problem as well, but check if your page redirects after submission.

我不确定这是否也是您的问题,但请检查您的页面在提交后是否重定向。

回答by Haritsinh Gohil

In Codeigniter we can check which HttpRequestit is using 2 below Inputclass' method :

在 Codeigniter 中,我们可以检查HttpRequest它正在使用以下Input类的2方法:

1. server('REQUEST_METHOD')

1. server('REQUEST_METHOD')

$this->input->server()is identical to Core PHP's $_SERVERvariable.

$this->input->server()与 Core PHP 的$_SERVER变量相同。

example:

例子:

if ($this->input->server('REQUEST_METHOD') == 'GET') {
   echo "It's GET Request";
} else if ($this->input->server('REQUEST_METHOD') == 'POST') {
   echo "It's POST Request";
}  

2. method()

2. method()

Since Codeigniter 3 we can use method()also for checking request type.

从 Codeigniter 3 开始,我们也method()可以用于检查请求类型。

method([$upper = FALSE])

方法([$upper = FALSE])

Parameters:$upper (bool) – Whether to return the request method name in upper or lower case

参数:$upper (bool) – 是否以大写或小写形式返回请求方法名称

Returns:HTTP request method

返回:HTTP 请求方法

Return type:string

返回类型:字符串

explanantion:It Returns the $_SERVER['REQUEST_METHOD'], with the option to set it in uppercase or lowercase.

说明:它返回$_SERVER['REQUEST_METHOD'],并可选择将其设置为大写或小写。

example:

例子:

echo $this->input->method(TRUE); // Outputs: POST
echo $this->input->method(FALSE); // Outputs: post
echo $this->input->method(); // Outputs: post