Update Div’s after database status is changed

Asked

Viewed 46 times

0

I have the following listing:

inserir a descrição da imagem aqui

When I upgrade from pending to paid status:

inserir a descrição da imagem aqui

I would like the values of the upper Paid and Pending Panels to be updated automatically, but only when I update the page:

inserir a descrição da imagem aqui

I’m trying this way:

HTML

<div class="col-lg-6 col-md-6">
  <div class="panel panel-green">
      <div class="panel-heading">
          <div class="row">
              <div class="col-xs-3" style="font-family: Arial; font-size: 30px">
                  <i class="fas fa-dollar-sign fa-2x"></i>
              </div>
              <div class="col-xs-9 text-right">
                  <div class="huge statusPagos" style="font-family: Arial; font-size: 30px">
                   <?php echo $metodos->visualizarPagamentos($status = 'P'); ?>
                  </div>
                  <div style="height: 14px"></div>
                  <div>Pagos</div>
              </div>
          </div>
      </div>
      <a href="<?php echo $caminhoAbsoluto; ?>/sistema/financeiro/?pagos">
          <div class="panel-footer" onclick="window.location.href='<?php echo $caminhoAbsoluto; ?>/sistema/ver-pagos/';" title="Ver pagamentos confirmados">
              <span class="pull-left">Visualizar</span>
              <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
              <div class="clearfix"></div>
          </div>
      </a>
  </div>
</div>
<div class="col-lg-6 col-md-6">
  <div class="panel panel-red">
      <div class="panel-heading">
          <div class="row">
              <div class="col-xs-3" style="font-family: Arial; font-size: 30px">
                  <i class="fas fa-dollar-sign fa-2x"></i>
              </div>
              <div class="col-xs-9 text-right">
                  <div class="huge statusAberto" style="font-family: Arial; font-size: 30px">
                    <?php
                    echo $metodos->visualizarPagamentos($status = 'A');
                    ?>
                  </div>
                  <div style="height: 14px"></div>
                  <div>Pendentes</div>
              </div>
          </div>
      </div>
      <a href="<?php echo $caminhoAbsoluto; ?>/sistema/financeiro/?pendentes">
          <div class="panel-footer" onclick="window.location.href='<?php echo $caminhoAbsoluto; ?>/sistema/ver-pendentes/';" title="Ver pagamentos pendentes">
              <span class="pull-left">Visualizar</span>
              <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
              <div class="clearfix"></div>
          </div>
      </a>
  </div>
</div>

Jquery

<script>
    $(document).on('click',"#btnAlterar", function(){
      var t;
      function temporiza(status){
         t = setTimeout(function(){
         $('.statusAberto').html('<?php echo $metodos->visualizarPagamentos('A'); ?>');
         $('.statusPagos').html('<?php echo $metodos->visualizarPagamentos('P'); ?>');
        },1000);
     }
      var that = this;
      var IdFinanceiro = $(this).attr('data-id');  // pega o id do botão
      $.post('sistema/alterar-status.php', {key: IdFinanceiro}, function(retorno){
      if(retorno == 1){
        $(that).parent('.status').html("<a href='#!' id='btnAlterar' data-id='<?php echo $csClientes->IdFinanceiro; ?>' class='btn btn-success btn-xs'>Pago</a>");
        if (!t) temporiza();
      }if(retorno == 0){
        $(that).parent('.status').html("<a href='#!' id='btnAlterar' data-id='<?php echo $csClientes->IdFinanceiro; ?>' class='btn btn-danger btn-xs'>Pendente</a>");
        if (!t) temporiza();
      }
     });
   });
  </script>

PHP

/**
* Método atualiza os valores dos pagamentos
* Encontra-se na página financeiro.php
* @access public
* @param string $status
* @return string $visualizar
*/
public function visualizarPagamentos($status){
   $sql = mysqli_query($this->conexao,"SELECT *, SUM(ValorTotal) AS ValorTotalPagto FROM cs_financeiro WHERE StatusPagamento = '".mysqli_real_escape_string($this->conexao,$status)."';");
   $csVisualizar = mysqli_fetch_object($sql);
   $pagamentos = ($csVisualizar->ValorTotalPagto == "")?"R$ 0,00":"R$ ".number_format($csVisualizar->ValorTotalPagto,2,',','.');
   return $pagamentos;
}

The update and status change is working normally in the listing, but the problem is only in the upper Panel’s, which do not automatically update the values.

  • It would basically change the color of the div and the text "paid" and "pending", that’s not it?

  • Hello Sam. Actually it would be the values that are within the div’s. Includes in my post the method visualizarPagamentos() PHP. It only brings up-to-date values.

  • I thought the value wouldn’t change, just the status.

  • You will need to create another asynchronous connection, generate these values using the method you created visualizarPagamentos() and update the respective Ivs. There is no other way.

  • Hello Andrei. Forgive me, I couldn’t understand. Would this asynchronous connection be in Jquery? If so, could you show me an example? 'Cause Jquery’s not really my strong suit.

  • Yes... I’ll make an example.

  • There’s actually another way to do it, which would be by taking the values from the list at the time of the click and updating the div. That would be better than my ayual response, but then you would need to put the rest of the html code.

  • Got it? Something calls here!

  • Ball show Andrei. I will test tonight and give you a return, combined?

  • Relax! Just use "@" to call me if I’m not notified. Type: @Andreicoelho

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.