yii2 GridView Filter how we change GET to POST

hi all…

my apologize to many ask.

in yii1, CGridView, in filter, if we insert something in filter then it normally search and working well. it use POST method to send data.

now in yii2, GridView, it use GET. all we put in filter will send to url. some user may not comfortable with GET method.

then, how we can set GET to POST. so data just send by POST. in which part and is it any issue will rise if change it to POST?

thank you for your help.

and thank you to all developers for this great framework :)

This comment shall solve your problem:


How should we get the params in the `search model

And i also read online in one of the comment that with post the pagination doesnt work properly sometimes as pagination is loosely coupled with GET

so what should we use there???

Hi bhavuksuthar23

I had the very same problem.

The solution is:

In your view file add pjax widget begin and end just before and after your grid, like this:

    <?php \yii\widgets\Pjax::begin(['id' => 'some-id-you-like', 

            'timeout' => false, 

            'enablePushState' => false, 

            'clientOptions' => ['method' => 'POST']]); ?>

    <?= GridView::widget([

    ... your gridview usual configuration...

    ... if you are using kartik gridview dont set 'pjax'=>true

    ]); ?>

    <?php \yii\widgets\Pjax::end(); ?>

And then in your controller replace:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);


$dataProvider = $searchModel->search(Yii::$app->request->post())

In my application it did the trick.

I wish you a good work.

We can create one hidden form with load all post parameters. get the pagination parameters using jquery. Push pagination parameter value into hidden form. trigger submit action using jquery.

        $form = ActiveForm::begin(['id' => 'pagination_link-form','options' => ['style'=>'display:none']]);
            foreach ($_REQUEST as $key => $value) {
               echo Html::hiddenInput($key,$value);
            echo Html::hiddenInput('page','',['id'=>'page']);
            echo Html::hiddenInput('per-page','',['id'=>'perpage']);
                 var actionurlold = $("#pagination_link-form").attr("action");
                function getUrlParameter(pageurl,sParam) {
                    var sPageURL = pageurl,
                        sURLVariables = sPageURL.split("&"),

                    for (i = 0; i < sURLVariables.length; i++) {
                        sParameterName = sURLVariables[i].split("=");

                        if (sParameterName[0] === sParam) {
                            return sParameterName[1] === undefined ? true : sParameterName[1];
                function removeURLParameter(url, parameter) {
                    //prefer to use l.search if you have a location/link object
                    var urlparts= url.split("?");   
                    if (urlparts.length>=2) {

                        var prefix= encodeURIComponent(parameter)+"=";
                        var pars= urlparts[1].split(/[&;]/g);

                        //reverse iteration as may be destructive
                        for (var i= pars.length; i-- > 0;) {    
                            //idiom for string.startsWith
                            if (pars[i].lastIndexOf(prefix, 0) !== -1) {  
                                pars.splice(i, 1);

                        url= urlparts[0]+"?"+pars.join("&");
                        return url;
                    } else {
                        return url;

                $(".pagination a").on("click",function(){
                    var page = getUrlParameter($(this).attr("href"),"page");
                    var perpage = getUrlParameter($(this).attr("href"),"per-page");
                    //$("#pagination_link-form #page").val(page);
                    //$("#pagination_link-form #perpage").val(perpage);
                    var actionurl = $("#pagination_link-form").attr("action");
                    actionurl = removeURLParameter(actionurl, "page");
                    actionurl = removeURLParameter(actionurl, "per-page");
                    actionurl = actionurl+"&page="+page+"&per-page="+perpage;
                    return false;

1 Like

Done. Working Fine.

1 Like

i did this, but when i type in anything to search in my gridview, it redirect to home page. any idea why?