CodeIgniter Forums
unexpected $this->input->post behavior - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: unexpected $this->input->post behavior (/showthread.php?tid=61152)



unexpected $this->input->post behavior - El Forum - 10-02-2014

[eluser]RobertSF[/eluser]
I'm using an array for my form fields. I've set validation rules according to the manual, and both form_error() and set_value() are working as expected. However, after I submit the form and run the validation successfully, I'm having trouble retrieving the values through the Input class.

When I execute this code:
Code:
if ($this->form_validation->run())
{
  var_dump($this->input->post());
  var_dump($this->input->post('glist'));
  var_dump($this->input->post('glist[name]'));
  exit;

I get this result:
Code:
array
  'glist' =>
    array
      'name' => string 'Motorcycle Club' (length=15)
      'gifts' => string '3' (length=1)
      'price' => string '2.50' (length=4)
  'submit' => string 'Ok' (length=2)

array
  'name' => string 'Motorcycle Club' (length=15)
  'gifts' => string '3' (length=1)
  'price' => string '2.50' (length=4)

boolean false

That is to say, I can retrieve the entire post array, and I can retrieve an array in the post array, but I can't seem retrieve elements from an array in the post array. I've tried various combinations like,
Code:
var_dump($this->input->post('glist['name']'));
var_dump($this->input->post("glist['name']"));
var_dump($this->input->post("glist[name]"));
var_dump($this->input->post('glist[\'name\']'));
but they all return false.

What is the correct way to retrieve array elements from the post array through the Input class?


unexpected $this->input->post behavior - El Forum - 10-02-2014

[eluser]CroNiX[/eluser]
Code:
$glist = $this->input->post('glist');
echo $glist['name'];



unexpected $this->input->post behavior - El Forum - 10-02-2014

[eluser]RobertSF[/eluser]
Thanks. I discovered this isn't a Codeigniter issue since you also can't retrieve array elements directly from the $_POST variable.

Code:
var_dump($_POST); // this works
var_dump($_POST['glist']); // this works too
var_dump($_POST['glist[name]']); // nope, doesn't work.

The last line of code returns an error:
Quote:Severity: Notice
Message: Undefined index: glist[name]

I'd never run into that because I'd never put arrays on the $_POST variable. Looks like Codeigniter makes you try more adventuresome things. Smile


unexpected $this->input->post behavior - El Forum - 10-03-2014

[eluser]InsiteFX[/eluser]
Code:
$data = $this->input->post(NULL, TRUE);

Returns all input post to $data.