[php] Codeigniter $this->input->post() empty while $_POST is working correctly

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!

This question is related to php codeigniter post

The answer is


Use

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

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


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


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

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


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)


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]


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

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


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


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


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.


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"


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

type: 'posts',

instead of

type: 'post',

Syntax, D'oh!


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>

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time