Codeigniter $this->input->post() empty while $_POST is working correctly
Asked Answered
P

14

20

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.

A print_r($_POST) gives all the variables fully and correctly?

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.

Any thoughts on how I should try to get this working?

Thanks!

Photograph answered 11/9, 2012 at 19:2 Comment(6)
Is this on a regular form or are you using AJAX?Streamline
Just a regular form at the momentPhotograph
what does var_dump($this->input->post()); show?Dominickdominie
Do you want to get all post data as an array, without any specific key or your $this->input->post('some_data') is not working ?Sokoto
Matthew - please post your controller and view code.Dominickdominie
I am experiencing the same issue. Did you find a solution jet?Sayette
B
30

You can check, if your view looks something like this (correct):

<form method="post" action="/controller/submit/">

vs (doesn't work):

<form method="post" action="/controller/submit">

Second one here is incorrect, because it redirects without carrying over post variables.

Explanation:

When url doesn't have slash in the end, it means that this points to a file.

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.

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.

Baste answered 28/5, 2013 at 16:0 Comment(5)
I was about to ask you what to do in a similar problem but using XMLHttpRequest. //wont work xhr.open("POST","/controller/doRegistration",true); //doing as you said... wont work xhr.open("POST","/controller/doRegistration/",true); //but... works xhr.open("POST",'/controller/doRegistration',true);Aniakudo
@Sobiaholic Yes! Now im using something like website.com/index.php/controller/index website.com/index.php/controller/catalog and that kind of things.Aniakudo
I hate to say that using '/index.php/controller/catalog' is doing ok. Not "/index.php/controller/catalog". Honestly dont know why. Between .httpaccess file and apostrophe '' Im confused.Aniakudo
both 1st and second are sme WTFIpsambul
@rahultyagi They are not the same - first one has slash in the end. I added explanation to my answer.Baste
L
5

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

Lipman answered 25/2, 2013 at 6:19 Comment(0)
S
5

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

Selfregard answered 8/9, 2017 at 20:40 Comment(1)
OK. This was actually my case! Thanks for the insight! I've recently added ssl to the website and the base_url was without www but my htaccess was forcing no www! :/Matabele
T
2

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

$config['index_page'] = ''; // ci 2.x
$config['index_page'] = 'index.php'; // ci 3.x

My .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>
Tipperary answered 17/2, 2017 at 15:47 Comment(0)
W
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

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

    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)

Wellturned answered 11/9, 2012 at 21:25 Comment(1)
When asking for clarification and diagnostics from the OP, use post comments under the question (you shouldn't be asking any questions in an answer).Pourparler
A
1

Use

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

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

Acidfast answered 17/12, 2016 at 14:44 Comment(0)
L
1

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

action="http://www.yourdomain.com/index.php/yourprocess"

Laresa answered 2/6, 2018 at 2:52 Comment(0)
M
0

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

type: 'posts',

instead of

type: 'post',

Syntax, D'oh!

Mohammadmohammed answered 29/7, 2016 at 18:39 Comment(1)
This is an entirely different issue because the OP says print_r($_POST) gives all the variables fully and correctly. I would invite you to post your own new question and answer it, but Typo questions are closed as off-topic on StackOverflow. This post should be removed as it is not focused on resolving the issue that the OP was having. Just because the title is vague doesn't mean that all possible failures receiving posted data should be stuffed into this page. Dear moderators, do not trash this comment as rude -- this is truth and is aimed at improving site content.Pourparler
S
0

I had a similar problem. In my case, I was POSTing to the CodeIgniter website via an outdated Python script and that script was using "http" URLs instead of "https". I had updated the website to use only https a while ago, but this script wasn't updated.

In short, using HTTP caused $this->input->post() to be an empty array.

Shoffner answered 10/1, 2023 at 18:35 Comment(0)
C
-1

You are missing the parent constructor. When your controller is loaded you must Call the parent CI_Controller class constructor in your controller constructor

Caboose answered 11/11, 2020 at 3:35 Comment(0)
C
-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.

A few solutions, stick to $_POST for retrieving all POST data, or assign variables from each POST item that you need, for example:

// 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!
}
Crossway answered 23/10, 2012 at 21:36 Comment(1)
Sorry but this is completely false. From the docs regardting $this->input->post() - "To return an array of all POST items call without any parameters." - what you're doing is completely unnecessary, and it also circumvents Codeigniter's built-in security measures. ellislab.com/codeigniter/user-guide/libraries/input.htmlMaribeth
F
-2

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

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

Fourdrinier answered 11/12, 2015 at 21:1 Comment(0)
M
-3

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

Machinate answered 15/3, 2013 at 11:32 Comment(0)
A
-3

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 .

abs, José Camargo]

Armrest answered 11/1, 2016 at 2:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.