CodeIgniter Forums

Full Version: stupid input problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]soupdragon[/eluser]
Ok I'm sure i am doing something wrong but i can't work out what !

In my controller i am not initializing the input class
cos ...This class is initialized automatically by the system so there is no need to do it manually.

but
print_R($_POST);
herewith i have data


whereas this is empty ?
print_R($this->input->post());

so what have i missed ??

El Forum

[eluser]xwero[/eluser]
the input post method doesn't mimic the $_POST global, it only grabs the value from the key you provide.

El Forum

[eluser]soupdragon[/eluser]
ahhh see i knew it was something stupid !

- so for others that means

print_R($this->input->post());
is always empty even if something is there
eg $this->input->post('genre')

El Forum

[eluser]webthink[/eluser]
Just to further clarify
$this->input->post() is a function (actually it's technically a class method) which returns true if $_POST has been initialized or false if it hasn't. print_r typically accepts variables such as arrays or objects. In you're case you've passed it a value TRUE or FALSE which is why there is no output.
when you pass it a parameter such as $this->input->post(’genre’) it returns the value which is why you're able to see output.

El Forum

[eluser]soupdragon[/eluser]
ahhhh i make progress :-)

thanks

El Forum

[eluser]Pygon[/eluser]
As webthink said -- print_r doesn't work for boolean values.

However -- $this->input->post() can *not* be used to test whether $_POST has been initialized.

post() without using a key name will always return FALSE regardless of anything:

Code:
function post($index = '', $xss_clean = FALSE)
{        
   if ( ! isset($_POST[$index]))
   {
      return FALSE;
   }
//....

$this->input->post('genre') will return the value, or FALSE if it is not set.

Personally, I'm of the opinion that $this->input->post() should return TRUE if not empty, FALSE if empty(or not set), the VALUE when supplied a key, or FALSE if the key is not set.

Which would allow:
Code:
if( $this->input->post() )
{
//Process form
}
else
{
//show form
}

El Forum

[eluser]xwero[/eluser]
[quote author="Pygon" date="1204670659"]Personally, I'm of the opinion that $this->input->post() should return TRUE if not empty, FALSE if empty(or not set), the VALUE when supplied a key, or FALSE if the key is not set.
[/quote]
I think that will be even more confusing for new comers. If there would be a check to confirm there are values in the $_POST global posted will be more intuitive
Code:
if($this->input->posted())
{
   echo $this->input->post('yes');
}

El Forum

[eluser]soupdragon[/eluser]
well i would agree with Pygon

would be more like normal php

if( $this->input->post() )
{
//Process form

is basically the same as
if (!empty($_POST))
{
// process form

and before i start a process routine would be nice to know if i need too ...

El Forum

[eluser]Pygon[/eluser]
[quote author="xwero" date="1204672681"]
I think that will be even more confusing for new comers. If there would be a check to confirm there are values in the $_POST global posted will be more intuitive
Code:
if($this->input->posted())
{
   echo $this->input->post('yes');
}
[/quote]

I agree and disagree. I don't think it would be terribly confusing although there may be a little confusion (however per my request, more detailed function documentation should handle it well).

My point is really, post() is acting as an accessor, so it would seem logical that the accessor would return TRUE (or the cleaned contents) if it contains data (!empty) and FALSE if it does not (empty), no different than calling empty on the $_POST array. It seems to me that the goal of input is to provide safe access to arrays, so I believe it should also be able to provide a safe array or at the least a boolean status of it.

El Forum

[eluser]xwero[/eluser]
[quote author="Pygon" date="1204676746"]My point is really, post() is acting as an accessor, so it would seem logical that the accessor would return TRUE (or the cleaned contents) if it contains data (!empty) and FALSE if it does not (empty), no different than calling empty on the $_POST array. It seems to me that the goal of input is to provide safe access to arrays, so I believe it should also be able to provide a safe array or at the least a boolean status of it.[/quote]
I understand your point but that should be added for the 3 array methods (post, cookie and server). The server global is always TRUE so i guess the best thing is to get the cleaned input.
Pages: 1 2