S
S
Sergey Bard2018-05-22 17:09:14
Yii
Sergey Bard, 2018-05-22 17:09:14

Why doesn't the view display?

Hello everyone, I don’t know how to do it anymore), in general I do ajax like this

$this->registerJs("
  $('.send').on('click', function() {
    if($(\"textarea[name='textarea']\").val()){
      $.ajax({
        type: 'POST',
        url: 'time-checker/complete-comment/',
        data: {
          val: $(\"textarea[name='textarea']\").val()
        }
      });
    } else {
      $(\"textarea[name='textarea']\").css({'border-color':'#d8512d'});
      setTimeout(function(){
        $(\"textarea[name='textarea']\").css({'border-color':'rgba(222, 222, 222, 1)'}); 
      },1000);
    }		
    $('.btn-outline, .close').on('click', function() {
      location.reload();
    });
  });
");

the controller is
public function actionCompleteComment()
    {		 
    if (Yii::$app->request->isAjax) {
      $data["time"] = UserTimeReport::find()->where(['user_id'=>Yii::$app->user->id, 'date'=>Yii::$app->params['dateToday']])->asArray()->one();
      $fst = time() - $data["time"]["start"];
      $new_time_work = $fst - $data["time"]["pause"];
      $model = UserTimeReport::findOne([
        'user_id'=>Yii::$app->user->id,
        'date'=>Yii::$app->params['dateToday']
      ]);
      $model->end = time();
      $model->work = $new_time_work;			
      $model->comment = $_POST['val'];
      $model->update();  

      $data["st"] = date("H:i:s", $data["time"]["start"]);
      $data["ed"] = date("H:i:s", $model->end);

      return $this->renderAjax('modal', ['data' => $data]);
        } else {
      throw new ForbiddenHttpException('что-то не так');
        }
    }

everything works except this
return $this->renderAjax('modal', ['data' => $data]);

those. it changes the data in the database as it should, but the view modal does not want to show, I have already tried everything I found, but nothing helps.
ps
modal code
<div class="modal modal-success" id="modal-success" style="display: none; padding-right: 17px;">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">×</span></button>
                <h4 class="modal-title">Вы завершили рабочий день!</h4>
              </div>
              <div class="modal-body">
                <table class="table">
          <thead>
            <tr>
              <th>Старт</th>
              <th>Пауза</th>
              <th>Завершил</th>
              <th>Работа</th>
              <th width="50">
              </th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><?= $data["st"]?></td>
              <td><?= $data["ps"]?></td>
              <td><?= $data["ed"]?></td>
              <td><?= $data["wk"]?></td>
            </tr>
          </tbody>
        </table>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-outline" data-dismiss="modal">Close</button>
              </div>
            </div>
            <!-- /.modal-content -->
          </div>
          <!-- /.modal-dialog -->
        </div>
    <?php
    $this->registerJs("
      $('#modal-success').modal('show');
      $('.btn-outline, .close').on('click', function() {
        location.reload();
      });
    ");
    ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2018-05-22
@fornit1917

You are making an ajax request and not processing the response from the server in any way. The server returns you the html that you rendered, but you need to write code in js in the ajax request handler that will show it.

E
Eugene, 2018-05-23
@evgen9586

Where is the response from the server?

$.ajax({
success: function (res) {
                   туть обработка при успехе
                },
                error: function () {
                   туть обработка при ошибке
                }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question