3

I'm using Kohana 3.1 and I'm getting a very strange error. The Kohana POST handler seems to think that there's an undefined index when there is not one.

Inside of a controller class:

$post = $this->request->post();
var_dump(isset($post['jid'])); //true
$jid = $post['jid']; //Undefined Index error.

If I actually use $post['jid'] it works fine, but it is annoying to not be able to assign it to a more convenient variable. Anyone have any idea what would cause this?

ErrorException [ Notice ]: Undefined index: jid
84       $jid = $post['jid'];

var_dump of $post:

array(4) { 
    ["jid"]   => string(1) "7" 
    ["topic"] => string(5) "Test1"
    ["entry"] => string(14) "CHECK ONE TWO" 
    ["enter"] => string(4) "POST" 
}
Petah
  • 45,477
  • 28
  • 157
  • 213
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 3
    `var_dump($post)` and paste that in your question please. – Petah Jun 07 '11 at 02:59
  • Are you sure that's where the error is occurring? Post the error message please – Phil Jun 07 '11 at 03:01
  • 1
    @tandu Are you seeing the dump above in the same request as the error? The only thing I can think of here is that you're attempting to read `$_POST` on a GET request – Phil Jun 07 '11 at 03:20
  • 2
    You must be doing something else your not telling us or there is a bug in your PHP, because that is syntactically correct and should not produce a `E_NOTICE`. – Petah Jun 07 '11 at 03:22
  • 1
    Use `Arr::get($_POST, 'jid', $default_value)`, its a very useful array function. – biakaveron Jun 07 '11 at 05:09

2 Answers2

1

Altough I believe this is a PEBKAC, you can use $this->request->post('jid') as a getter for that variable.

Maybe you are just skipping the actual POST check?

Community
  • 1
  • 1
Kemo
  • 6,942
  • 3
  • 32
  • 39
  • Thanks for knowledge about the post check. I would upvote if not for you being a jerk. – Explosion Pills Jun 07 '11 at 13:47
  • @tandu: I don't see what exactly makes me a jerk, I consider it common sense – Kemo Jun 08 '11 at 07:58
  • You say "I believe this is a PEBKAC." Hey, we all make mistakes programming..insulting people is not helpful. – Explosion Pills Jun 14 '11 at 12:55
  • @tandu: that's not an insult, I'm really sorry if you thought of it that way. I'm just used to using the term on daily basis, it's not something I use to insult anyone – Kemo Jun 14 '11 at 13:00
0

Sometimes the problem is not as obvious as the error. The page was 302-redirecting (no post) to itself after completing the action where the post check is done. In fact, the initial post went through properly, but be careful about calling a method on $this when you mean to use another object (at least when $this is a controller). It's still unclear to me why Kohana didn't crash.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405