开发者

$_post in Kohana controller

开发者 https://www.devze.com 2023-02-14 22:07 出处:网络
i was wondering if i can get a variable with $_post, in a kohana controller if the controller doesn\'t \'control\' a form.

i was wondering if i can get a variable with $_post, in a kohana controller if the controller doesn't 'control' a form.

So, if i insert in a view something like:

 <form name="ordering" id="ordering" method="post" action="">
 <input type="hidden" id="ordering" value="0">  
 <select id="ordering" name="ordering">   
....

in the controller i put :

        $ordering = $_POST['ordering']; 

but gives me an error

or

       if ($this->request->method == 'POST') {    
        $ordering = $_POST['ordering'];
    }

but in this case it never gets there(at this bunch of code).

so my question is: how can i retriev开发者_运维知识库e in a controller a $_post variable if the controller doesn't handle only a form? thank you!


Kohana 3.0 :

if ($_POST)
{
$ordering = arr::get($_POST, 'ordering');
...

Kohana 3.1 :

if ($ordering = $this->request->post('ordering')) // or just $this->request->post()
{
...


PHP will issue a notice if you attempt to access an undefined array element. So if the "ordering" form was never submitted, attempting to access $_POST['ordering'] will result in

PHP Notice:  Undefined index: ordering in ...

Kohana's Arr class provides a nice helper method to get around this.

If you call

$ordering = Arr::get($_POST, 'ordering', 0);

It will retrieve the ordering value from the post variable. If $_POST['ordering'] is not set, it will return the third parameter instead. You can then try if ($ordering) ...

This is useful for $_POST/$_GET arrays, or any function that accepts arrays – it allows you to concisely specify a fallback behavior rather than having to test with isset.

One of the advantages of Kohana is that the source code tends to be very clean and easy to understand (which is nice because documentation is sparse.) I'd suggest you take a check out the Kohana_Arr class and look at the methods available!


ID's are unique! Use class insted or different IDs.

Your form and the select both have got ordering, change one to something else, like:

<form name="ordering_form" id="ordering_form" method="post" action="">
<input type="hidden" id="ordering_input" value="0">  
<select id="ordering" name="ordering">
...
</select>
</form>

and in your Kohana Controller:

if( isset( $_POST['ordering'] ) )
{
    $ordering = $_POST['ordering'];
}

this should work, because i cant find any other error

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号