Codeigniter $this->input->post is always FALSE
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
Mj1992
Updated on March 28, 2020Comments
-
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
- 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 toget
everything 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 over 10 yearsuse
isset($_POST)
to check data. -
Bora over 10 years
var_dump($_POST);
isNULL
? -
Tschallacka over 10 yearsAnd you should also check with the name of you submit button isset($_POST['submitname'])
-
Himanshu Pandey over 10 yearsuse
echo '<pre>';print_r($_POST);
to show post data -
Mj1992 over 10 years
var_dump
returns empty array like thisarray(0) {}
-
Bora over 10 yearsI guess, your issue about on
HttpRequest
. -
Himanshu Pandey over 10 yearsuser
isset($_REQUEST)
to check data -
Fu Xu over 10 yearswhat's the result of
var_dump($GLOBALS['HTTP_RAW_POST_DATA'])
?REST REQUEST
is not a normal POST REQUEST -
Pattle over 10 yearsDo you have crsf protection enabled?
-
Mj1992 over 10 yearswhen 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 over 10 yearsthan you need to specify the name of the variable post. see updated answer
-
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 toget
instead ofpost
. -
Harshal Mahajan over 10 yearsyou should check the response using var_dump($_REQUEST);
-
Himanshu Pandey over 10 years@Mj i think you receive json data
-
Mj1992 over 10 yearsOK 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 ?