0

I have a view in which I have two buttons. While clicking on any of the two buttons, a certain request is made.

View

<?php Pjax::begin(); ?>
        <?=Html::beginForm(['process'],'post');?>
        <?=Html::submitButton('Disconnect', ['id'=>'d','name'=>'dco','class' => 'btn btn-primary']);?>
        <?=Html::submitButton('Connect', ['id'=>'r','name'=>'rco','class' => 'btn btn-info']);?>
        <br><br>
        <div class="pre-scrollable">
        <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [


        ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($d) {
            return ['value' => $d['msn']];
        }],


        'customer_id',
       // 'dept_code:ntext',
        'dept_name:ntext',
        'sub_div_name',
        [
            'label' => 'Sub Division Name',
            'value' => function ($d) {
                return $d->subdiv->name;
            },
            'filter' => Html::activeDropDownList($searchModel, 'sub_div_code', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),

        ],
        'division_name',
        'allowed_units',
        'msn',

        'units_consumed',
        [
            'label' => 'Disconnected',
            'attribute' => 'disconnected',
            'format'=>'raw',
            'contentOptions' => ['style'=>'text-align:center'],
            'value' => function($model){
                return $model->disconnected == 1 ? '<span class="glyphicon glyphicon-ok text-success"></span>' : '<span class="glyphicon glyphicon-remove text-danger"></span>';
            },
            'filter' => Html::activeDropDownList($searchModel, 'disconnected', [''=>'All','1'=>'Yes','0'=>'No'], ['class' => 'form-control']),
        ],


        'active_energy_total_m',


        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>
        </div>
        <?= Html::endForm();?>
        <?php Pjax::end(); ?>

Controller

 public function actionProcess()
{

 $soapUrl = "http://ip:port/HES/services/Request";
    $userName = "user";
    $password = "123456";

 if (isset($_POST['dco'])) 
    {
        if(Yii::$app->request->isPost)
        {
            $data = Yii::$app->request->post('selection'); //checkbox (array)


            foreach($data as $value)
            {

                $msn = $value;


                $xml_post_string = /** @lang text */
                    '//soap request';

                $headers = array(
                    "Content-type: text/xml;charset=\"utf-8\"",
                    "Accept: text/xml",
                    "Cache-Control: no-cache",
                    "Pragma: no-cache",
                    "Content-length: ".strlen($xml_post_string),
                ); //SOAPAction: your op URL

                $url = $soapUrl;

                // PHP cURL  for https connection

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

                // converting
                $response = curl_exec($ch);
                curl_close($ch);
                $domd=new DOMDocument();

                var_dump($response);
                if(!$domd->loadXML($response)){
                    throw new \RuntimeException("failed to parse XML!");
                }
                $inner_xml=$domd->getElementsByTagName("return")->item(0)->textContent;
                if(!($domd2=@DOMDocument::loadXML($inner_xml))){
                    throw new \RuntimeException("failed to parse inner_xml!");
                }
                $AsyncReplyFlag=$domd2->getElementsByTagName("AsyncReplyFlag")->item(0)->textContent;

                if ($AsyncReplyFlag =='true')
                {
                    $ds = 1;

                    $disconnected_at = date('Y-m-d H:i:s');

                    try {
                        Yii::$app->db->createCommand(/** @lang text */
                            "update 
  `accurate_mam`.`daily_log` 
set
  `disconnected` = '$ds',
  `diconnected_at` = '$disconnected_at', 
  `reconnected_at` = NULL
where `msn` = '$msn' ;

")->execute(); //update master table

                        Yii::$app->db->createCommand(/** @lang text */
                            "update 
                        `accurate_mam`.`log_disconnected` 
                         set

                        `disconnected_at` = '$disconnected_at' 
                         where `msn` = '$msn'")->execute();// update log disconnected table
                    } catch (Exception $e) {
                    } // updating the master table
                }


            }

        }
    }
    }

What I want to do?

On button click, I want to show a spinner/loader that will start when the request is made and stop when the request is completed.

Note

I don't want to add a default time for spinner/loader. It should starts when the request is sent and stop when it's completed i.e. when the process is completed

I have searched many articles but can't find the complete implementation details.

Update 1

I have found a solution here and tried to implement it by doing the following

<style>
#loader {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.05) url("<?php \yii\helpers\Url::to('@web/images/25.png', true);  ?>") no-repeat center center;
    z-index: 10000;
}
</style>

JS

$(document).ready(function () { 

      $('form').submit(function(e) {
       e.preventDefault();      

        spinner.show();
        $.ajax({
            url:'$url',
            data: $(this).serialize()
            type: 'POST',
            dataType: 'JSON'
        }).done(function(resp) {
            spinner.hide();
          //alert(resp.status);
        });

  });      


}); 

Problem Facing

Now I am able to see a spinner but the process is not completed. After further debugging, I observed that I am getting bool(false) in var_dump(isset($_POST['dco'])); die(); while submitting the request

Update 2

I have found one more solution and tried to do the same

  1. Updating both buttons

    <?= Html::submitButton(Yii::t('app', '<i class="fa fa-times"></i>&nbsp;Disconnect'), ['class' => 'btn red', 'name' => 'dco', 'value' => '0']) ?>
    <?= Html::submitButton(Yii::t('app', '<i class="fa fa-check"></i>&nbsp;Connect'), ['class' => 'btn blue', 'name' => 'rco', 'value' => '1']) ?>
    

JS

 $('form').submit(function(e) {
       e.preventDefault();
        var strValue = "";        
    $('input[name="selection[]"]:checked').each(function() {

    if(strValue!=="")
        {
        strValue = strValue + " , " + this.value;

        }
    else 
       strValue = this.value;     

});


        spinner.show();
        $.ajax({
            url:'$url',
            data: {data :strValue},
            type: 'POST',
            dataType: 'JSON'
        }).done(function(resp) {
            spinner.hide();
          //alert(resp.status);
        });

  });      

Controller

if (Yii::$app->request->post()) {

   if (Yii::$app->request->post('dco') == 0) {
    //Code for value 0
 }

   if (Yii::$app->request->post('rco') == 1) {
    //Code for value 1
  }

 }

Problem Facing

When I click any of the two buttons it always hit the dco check i.e. if (Yii::$app->request->post('dco') == 0) it's not hitting the rco condition

There must be something that I am missing.

Any help would be highly appreciated.

Moeez
  • 494
  • 9
  • 55
  • 147
  • as far i understand its a normal form `submit` button which is submitting the form to the action, pjax has nothing to do with it or Ajax. – Muhammad Omer Aslam May 26 '19 at 17:18
  • and your question is very similar to this [question](https://stackoverflow.com/questions/46828614/how-to-add-spinner-loader-in-yii2-php) i wonder which one i should answer :P , or you can close your question in the other quedtion's favour if you think its the similar situation and i will add an answer there if none others work – Muhammad Omer Aslam May 26 '19 at 23:33
  • @MuhammadOmerAslam this one is original ;), also I did try to implement a [soluiton](http://www.marcorpsa.com/ee/t2228.html). Now I am able to see a loading screen but I am getting `null` at `isset($_POST['dco'])` – Moeez May 28 '19 at 03:26
  • @MuhammadOmerAslam I have added the updates about what I have tried – Moeez May 28 '19 at 06:26
  • added an answer fo you see if it works – Muhammad Omer Aslam May 28 '19 at 10:25
  • Can you mark the answer that worked for you? – Muhammad Omer Aslam Jun 11 '19 at 13:14

2 Answers2

1
<?php
$this->registerJs(<<<JS
$(document).on('pjax:send', function() {
   $('#loading').show();
});
$(document).on('pjax:complete', function() {
  $('#loading').hide();
});
JS
);
?>

example: with fontawesome( or can use image ....)

<i id='loading' class='fa fa-spinner fa-pulse' style='display:none;'></i>

You can also do this by a variable (in the controller).

user206
  • 1,085
  • 1
  • 10
  • 15
  • Read the comment before. Place the **html** and **registerJs** (Top codes) in the VIEW file(GridView) and then delete your javascript code. I mean `event.preventDefault () .....` Now test the top **loading** By **submitButton** (Send **form** by **pjax**) – user206 May 28 '19 at 16:44
  • You are right . data-pjax should be empty (`data-pjax' => ''`). It is not possible to receive the submitButton id for your code. I put another post. Test the code. I hope this helps. Otherwise, use the idea of @Muhammad Omer Aslam – user206 May 29 '19 at 03:12
1

My Understanding

Looking at your current implementation you are using normal form submission although wrapped inside the Pjax container but you haven't enabled form submission via Pjax for your form and want to show a spinner or loader that would show until the request to the action where you are sending the curl request to some API completes.

Limitations

Something you need to understand is that if you are making normal form submission and trying to show a spinner for some process that you are doing inside your controller/action won't work as the view is not rendered yet and your spinner related any javascript or CSS isn't rendered in the browser yet and won't show.

Solution

On the other hand, if you use ajax or pjax for your form submissions as you already have wrapped your form inside the Pjax container you can use the Pjax events with enabling a spinner from the AdminBSBTheme which uses CSS only to show the spinner.

That won't just work for your ajax forms but you can use it overall the site for normal page loads leaving a nice effect. It automatically hides once the DOM is ready, in case of slow internet connections it makes things look more aligned.

Enable Pjax on Form

You have to add data-pjax="1" attribute in the form tag to enable Pjax form submission it won't work by default, so go ahead and change the following line to enable Pjax submission for the form.

<?php echo Html::beginForm(['process'],'post', ['data' => ['pjax' => 1]);?>

Enable Timeout for Pjax

Then you need to specify the timeout parameter property if your curl request takes a bit of time it's better to add high value for the timeout property so that it won't throw loading request failed or timeout error and waits for the response. Let us give it 10 seconds so if the request takes up to 10 secs to complete it should keep waiting otherwise continue if the response received

<?php Pjax::begin(['timeout'=>10000]); ?>

Spinner Integration

Now comes the part of the spinner integration you can see it running in the demo below it is pure CSS based.

Copy the Html from the demo below in your views\layouts\main.php file after $this->beginBody() and add the css inside your site.css file or, create a separate file and include in the AppAssets.php file.

/ Page Loader ================================= /

.page-loader-wrapper {
  z-index: 99999999;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background: #eee;
  overflow: hidden;
  text-align: center;
}

.page-loader-wrapper p {
  font-size: 13px;
  margin-top: 10px;
  font-weight: bold;
  color: #444;
}

.page-loader-wrapper .loader {
  position: relative;
  top: calc(50% - 30px);
}


/ Preloaders ================================== /

.md-preloader .pl-red {
  stroke: #F44336;
}

.md-preloader .pl-pink {
  stroke: #E91E63;
}

.md-preloader .pl-purple {
  stroke: #9C27B0;
}

.md-preloader .pl-deep-purple {
  stroke: #673AB7;
}

.md-preloader .pl-indigo {
  stroke: #3F51B5;
}

.md-preloader .pl-blue {
  stroke: #2196F3;
}

.md-preloader .pl-light-blue {
  stroke: #03A9F4;
}

.md-preloader .pl-cyan {
  stroke: #00BCD4;
}

.md-preloader .pl-teal {
  stroke: #009688;
}

.md-preloader .pl-green {
  stroke: #4CAF50;
}

.md-preloader .pl-light-green {
  stroke: #8BC34A;
}

.md-preloader .pl-lime {
  stroke: #CDDC39;
}

.md-preloader .pl-yellow {
  stroke: #ffe821;
}

.md-preloader .pl-amber {
  stroke: #FFC107;
}

.md-preloader .pl-orange {
  stroke: #FF9800;
}

.md-preloader .pl-deep-orange {
  stroke: #FF5722;
}

.md-preloader .pl-brown {
  stroke: #795548;
}

.md-preloader .pl-grey {
  stroke: #9E9E9E;
}

.md-preloader .pl-blue-grey {
  stroke: #607D8B;
}

.md-preloader .pl-black {
  stroke: #000000;
}

.md-preloader .pl-white {
  stroke: #ffffff;
}

.preloader {
  display: inline-block;
  position: relative;
  width: 50px;
  height: 50px;
  -webkit-animation: container-rotate 1568ms linear infinite;
  -moz-animation: container-rotate 1568ms linear infinite;
  -o-animation: container-rotate 1568ms linear infinite;
  animation: container-rotate 1568ms linear infinite;
}

.preloader.pl-size-xl {
  width: 75px;
  height: 75px;
}

.preloader.pl-size-l {
  width: 60px;
  height: 60px;
}

.preloader.pl-size-md {
  width: 50px;
  height: 50px;
}

.preloader.pl-size-sm {
  width: 40px;
  height: 40px;
}

.preloader.pl-size-xs {
  width: 25px;
  height: 25px;
}

.spinner-layer {
  position: absolute;
  width: 100%;
  height: 100%;
  border-color: #F44336;
  -ms-opacity: 1;
  opacity: 1;
  -webkit-animation: fill-unfill-rotate 5332ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  -moz-animation: fill-unfill-rotate 5332ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  -o-animation: fill-unfill-rotate 5332ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  animation: fill-unfill-rotate 5332ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
}

.spinner-layer.pl-red {
  border-color: #F44336;
}

.spinner-layer.pl-pink {
  border-color: #E91E63;
}

.spinner-layer.pl-purple {
  border-color: #9C27B0;
}

.spinner-layer.pl-deep-purple {
  border-color: #673AB7;
}

.spinner-layer.pl-indigo {
  border-color: #3F51B5;
}

.spinner-layer.pl-blue {
  border-color: #2196F3;
}

.spinner-layer.pl-light-blue {
  border-color: #03A9F4;
}

.spinner-layer.pl-cyan {
  border-color: #00BCD4;
}

.spinner-layer.pl-teal {
  border-color: #009688;
}

.spinner-layer.pl-green {
  border-color: #4CAF50;
}

.spinner-layer.pl-light-green {
  border-color: #8BC34A;
}

.spinner-layer.pl-lime {
  border-color: #CDDC39;
}

.spinner-layer.pl-yellow {
  border-color: #ffe821;
}

.spinner-layer.pl-amber {
  border-color: #FFC107;
}

.spinner-layer.pl-orange {
  border-color: #FF9800;
}

.spinner-layer.pl-deep-orange {
  border-color: #FF5722;
}

.spinner-layer.pl-brown {
  border-color: #795548;
}

.spinner-layer.pl-grey {
  border-color: #9E9E9E;
}

.spinner-layer.pl-blue-grey {
  border-color: #607D8B;
}

.spinner-layer.pl-black {
  border-color: #000000;
}

.spinner-layer.pl-white {
  border-color: #ffffff;
}

.right {
  float: right !important;
}

.gap-patch {
  position: absolute;
  top: 0;
  left: 45%;
  width: 10%;
  height: 100%;
  overflow: hidden;
  border-color: inherit;
}

.gap-patch.circle {
  width: 1000%;
  left: -450%;
}

.circle-clipper {
  display: inline-block;
  position: relative;
  width: 50%;
  height: 100%;
  overflow: hidden;
  border-color: inherit;
}

.circle-clipper .circle {
  width: 200%;
  height: 100%;
  border-width: 3px;
  border-style: solid;
  border-color: inherit;
  border-bottom-color: transparent !important;
  -ms-border-radius: 50%;
  border-radius: 50%;
  -webkit-animation: none;
  animation: none;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
}

.circle-clipper.left .circle {
  left: 0;
  border-right-color: transparent !important;
  -webkit-transform: rotate(129deg);
  -moz-transform: rotate(129deg);
  -ms-transform: rotate(129deg);
  -o-transform: rotate(129deg);
  transform: rotate(129deg);
  -webkit-animation: left-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  -moz-animation: left-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  -o-animation: left-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  animation: left-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
}

.circle-clipper.right .circle {
  left: -100%;
  border-left-color: transparent !important;
  -webkit-transform: rotate(-129deg);
  -moz-transform: rotate(-129deg);
  -ms-transform: rotate(-129deg);
  -o-transform: rotate(-129deg);
  transform: rotate(-129deg);
  -webkit-animation: right-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  -moz-animation: right-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  -o-animation: right-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
  animation: right-spin 1333ms cubic-bezier(0.4, 0, 0.2, 1) infinite both;
}

@-webkit-keyframes container-rotate {
  to {
    -webkit-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    -ms-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@keyframes container-rotate {
  to {
    -moz-transform: rotate(360deg);
    -ms-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@-webkit-keyframes fill-unfill-rotate {
  12.5% {
    -webkit-transform: rotate(135deg);
    transform: rotate(135deg);
  }
  25% {
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }
  37.5% {
    -webkit-transform: rotate(405deg);
    transform: rotate(405deg);
  }
  50% {
    -webkit-transform: rotate(540deg);
    transform: rotate(540deg);
  }
  62.5% {
    -webkit-transform: rotate(675deg);
    transform: rotate(675deg);
  }
  75% {
    -webkit-transform: rotate(810deg);
    transform: rotate(810deg);
  }
  87.5% {
    -webkit-transform: rotate(945deg);
    transform: rotate(945deg);
  }
  to {
    -webkit-transform: rotate(1080deg);
    transform: rotate(1080deg);
  }
}

@keyframes fill-unfill-rotate {
  12.5% {
    transform: rotate(135deg);
  }
  25% {
    transform: rotate(270deg);
  }
  37.5% {
    transform: rotate(405deg);
  }
  50% {
    transform: rotate(540deg);
  }
  62.5% {
    transform: rotate(675deg);
  }
  75% {
    transform: rotate(810deg);
  }
  87.5% {
    transform: rotate(945deg);
  }
  to {
    transform: rotate(1080deg);
  }
}

@-webkit-keyframes left-spin {
  from {
    -webkit-transform: rotate(130deg);
    -moz-transform: rotate(130deg);
    -ms-transform: rotate(130deg);
    -o-transform: rotate(130deg);
    transform: rotate(130deg);
  }
  50% {
    -webkit-transform: rotate(-5deg);
    -moz-transform: rotate(-5deg);
    -ms-transform: rotate(-5deg);
    -o-transform: rotate(-5deg);
    transform: rotate(-5deg);
  }
  to {
    -webkit-transform: rotate(130deg);
    -moz-transform: rotate(130deg);
    -ms-transform: rotate(130deg);
    -o-transform: rotate(130deg);
    transform: rotate(130deg);
  }
}

@keyframes left-spin {
  from {
    -moz-transform: rotate(130deg);
    -ms-transform: rotate(130deg);
    -o-transform: rotate(130deg);
    -webkit-transform: rotate(130deg);
    transform: rotate(130deg);
  }
  50% {
    -moz-transform: rotate(-5deg);
    -ms-transform: rotate(-5deg);
    -o-transform: rotate(-5deg);
    -webkit-transform: rotate(-5deg);
    transform: rotate(-5deg);
  }
  to {
    -moz-transform: rotate(130deg);
    -ms-transform: rotate(130deg);
    -o-transform: rotate(130deg);
    -webkit-transform: rotate(130deg);
    transform: rotate(130deg);
  }
}

@-webkit-keyframes right-spin {
  from {
    -webkit-transform: rotate(-130deg);
    -moz-transform: rotate(-130deg);
    -ms-transform: rotate(-130deg);
    -o-transform: rotate(-130deg);
    transform: rotate(-130deg);
  }
  50% {
    -webkit-transform: rotate(5deg);
    -moz-transform: rotate(5deg);
    -ms-transform: rotate(5deg);
    -o-transform: rotate(5deg);
    transform: rotate(5deg);
  }
  to {
    -webkit-transform: rotate(-130deg);
    -moz-transform: rotate(-130deg);
    -ms-transform: rotate(-130deg);
    -o-transform: rotate(-130deg);
    transform: rotate(-130deg);
  }
}

@-moz-keyframes right-spin {
  from {
    -moz-transform: rotate(-130deg);
    -ms-transform: rotate(-130deg);
    -o-transform: rotate(-130deg);
    -webkit-transform: rotate(-130deg);
    transform: rotate(-130deg);
  }
  50% {
    -moz-transform: rotate(5deg);
    -ms-transform: rotate(5deg);
    -o-transform: rotate(5deg);
    -webkit-transform: rotate(5deg);
    transform: rotate(5deg);
  }
  to {
    -moz-transform: rotate(-130deg);
    -ms-transform: rotate(-130deg);
    -o-transform: rotate(-130deg);
    -webkit-transform: rotate(-130deg);
    transform: rotate(-130deg);
  }
}

@keyframes right-spin {
  from {
    -moz-transform: rotate(-130deg);
    -ms-transform: rotate(-130deg);
    -o-transform: rotate(-130deg);
    -webkit-transform: rotate(-130deg);
    transform: rotate(-130deg);
  }
  50% {
    -moz-transform: rotate(5deg);
    -ms-transform: rotate(5deg);
    -o-transform: rotate(5deg);
    -webkit-transform: rotate(5deg);
    transform: rotate(5deg);
  }
  to {
    -moz-transform: rotate(-130deg);
    -ms-transform: rotate(-130deg);
    -o-transform: rotate(-130deg);
    -webkit-transform: rotate(-130deg);
    transform: rotate(-130deg);
  }
}
<!-- Page Loader -->
<div class="page-loader-wrapper">
  <div class="loader">
    <div class="preloader">
      <div class="spinner-layer pl-red">
        <div class="circle-clipper left">
          <div class="circle"></div>
        </div>
        <div class="circle-clipper right">
          <div class="circle"></div>
        </div>
      </div>
    </div>
    <p>Please wait...</p>
  </div>
</div>
<!-- #END# Page Loader -->

Then add the following on top of the views\layouts\main.php file

$js = <<<JS
     setTimeout(function () {
        $('.page-loader-wrapper').fadeOut();
    }, 50);
JS;
$this->registerJs($js, \yii\web\View::POS_READY);

Note: Ideally you can wrap everything related to the spinner in a separate asset file and call it in the main layout, but I will leave that part to you.

Until this point, if you try to click any of the links on your application you will see the spinner working for your normal page loads like the home page about or any other.

Enabling for Pjax requests

We need to enable it for the Pjax too, so go to your view file and add the following piece of javascript on top which uses the pjax:beforeSend event to show the spinner

$js = <<<JS
$(document).on("pjax:beforeSend",function(){
    $('.page-loader-wrapper').fadeIn();
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);

You don't need to worry about closing the spinner it would be closed once the page is loaded after the curl response via our main Layout code.

Update

The reason why the submit button value isn't submitted when using pjax is a known issue, see here. What you can do about it is to add 2 hidden fields for the 2 submit buttons and updated the value of the respective hidden field associated with the button when clicked and submit them along with the form and check the value for those new hidden inputs, so that you can identify which button was clicked.

So, first we will add hidden inputs, note the options used for the original submitButton rco and dco the id should be there.

<?php echo Html::beginForm(['about'], 'post', ['data' => ['pjax' => 1]]); ?>
<?php echo Html::submitButton('Disconnect', ['class' => 'btn btn-success', 'id' => 'dco']) ?>
<?php echo Html::submitButton('Connect', ['class' => 'btn btn-info', 'id' => 'rco']) ?>
<?php echo Html::hiddenInput('dco', '', ['id' => 'dco_input']); ?>
<?php echo Html::hiddenInput('rco', '', ['id' => 'rco_input']); ?>

Now we will replace the javascript that we added before in our view wiith the following

$js = <<<JS

    function loader(){
        $('.page-loader-wrapper').fadeOut();
        //reset the input values
        $("#dco_input,#rco_input").val('');

        //assign respective values to the associated hidden fields
        $("#rco,#dco").on('click',function(e){
            var buttonId = $(this).attr('id');
            $("#"+buttonId+"_input").val(1);
        });
    }
    
    $(document).on("pjax:beforeSend",function(e){
        $('.page-loader-wrapper').fadeIn();
    }).on("pjax:end",function(){
        loader();
    });
    loader();
JS;
    $this->registerJs($js, View::POS_READY);
?>

Now whenever we will click the dco submit button the hidden input dco_input will be populated with 1 and when the rco submit button is clicked rco_input will be updated with value 1 before the for is submitted and you will receive the value in post for both the buttons.

The last thing you need to change are the checks in your controller/action process(), which are checking isset($_POST['dco']) and isset($_POST['rco']) which will not work and need to be updated

if(isset($_POST['dco'])) 

with

if(isset($_POST["dco"]) && $_POST['dco']==1)

and

if(isset($_POST['rco'])) 

with

if(isset($_POST['rco']) && $_POST['rco']==1) 
Community
  • 1
  • 1
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68