Yii Framework Forum

How to get Post multiple array


(Adi Apriyanto) #1
<input type="hidden" name="arr[]" value="1"/>
<input type="hidden" name="arr[]" value="2"/>
<input type="hidden" name="arr[]" value="3"/>

im trying to get with Yii::$app->request->post(‘arr’) but empty results


(Mehdi Achour) #2

It should work. Is your form configured to do a POST? (ie. method="post")

When you use your browser inspector, how do you see the value being sent?
Look for something like this:
05%20AM


(Adi Apriyanto) #3

I got solutions with ajax

$.ajax({
    url: $(this).attr('href'),
    type: 'post',
    data: $('#w0').serialize(),
    success: function(data) {
        console.log(data);
    }
});

and the controller:

if (Yii::$app->request->isAjax) {
    $data = Yii::$app->request->post();
    return var_post($data->arr)
}

thanks @machour


(Adi Apriyanto) #4

Another solution I found on the doc
https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests

$request = Yii::$app->request; 

// returns all parameters 
$params = $request->bodyParams; 

// returns the parameter "arr" 
$param = $request->getBodyParam('arr');