• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unexpected $this->input->post behavior

#1
[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?

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

#3
[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

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

Returns all input post to $data.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.