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

58,631

Solution 1

you should try

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

to check data is coming or not

Solution 2

$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 :

if(isset($_POST))

or

  if(!empty($_POST))

See POST

you can also do this:

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

Solution 3

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

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";
    }
}

Solution 4

Try

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

}
else
{

}

Solution 5

In Codeigniter we can check which HttpRequest it is using 2 below Input class' method :

1. server('REQUEST_METHOD')

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

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()

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

method([$upper = FALSE])

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

Returns: HTTP request method

Return type: string

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

example:

echo $this->input->method(TRUE); // Outputs: POST
echo $this->input->method(FALSE); // Outputs: post
echo $this->input->method(); // Outputs: post
Share:
58,631
Mj1992
Author by

Mj1992

Updated on March 28, 2020

Comments

  • Mj1992
    Mj1992 about 4 years

    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 POST variables .

    • 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.

    What could be the issue ? Post Request is not working anywhere throughout this 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

    • Himanshu Pandey
      Himanshu Pandey over 10 years
      use isset($_POST) to check data.
    • Bora
      Bora over 10 years
      var_dump($_POST); is NULL?
    • Tschallacka
      Tschallacka over 10 years
      And you should also check with the name of you submit button isset($_POST['submitname'])
    • Himanshu Pandey
      Himanshu Pandey over 10 years
      use echo '<pre>';print_r($_POST); to show post data
    • Mj1992
      Mj1992 over 10 years
      var_dump returns empty array like this array(0) {}
    • Bora
      Bora over 10 years
      I guess, your issue about on HttpRequest.
    • Himanshu Pandey
      Himanshu Pandey over 10 years
      user isset($_REQUEST) to check data
    • Fu Xu
      Fu Xu over 10 years
      what's the result of var_dump($GLOBALS['HTTP_RAW_POST_DATA'])? REST REQUEST is not a normal POST REQUEST
    • Pattle
      Pattle over 10 years
      Do you have crsf protection enabled?
  • Mj1992
    Mj1992 over 10 years
    when I use if with get similarly as I've done above . It works. If you are saying that $this->input->post() will always return false than how is it working for $this->input->get() ?
  • Harshal Mahajan
    Harshal Mahajan over 10 years
    than you need to specify the name of the variable post. see updated answer
  • Mj1992
    Mj1992 over 10 years
    @DesertP I am using REST Console chrome extension to send requests. The same thing is working flawlessly when I change variables to get instead of post .
  • Harshal Mahajan
    Harshal Mahajan over 10 years
    you should check the response using var_dump($_REQUEST);
  • Himanshu Pandey
    Himanshu Pandey over 10 years
    @Mj i think you receive json data
  • Mj1992
    Mj1992 over 10 years
    OK I created a new script and still $_POST not working but $_GET is working . I think it is disabled in php. Can you tell me where to enable it ?