When you change a certain status, it changes from all

0

How can I get the user by clicking Pending , the status changes to Payment ? See:

I'mdoingthefollowing:

PHP

while($csClientes=mysqli_fetch_object($sqlClientes)){$visualizar.="<tr>";
            $visualizar .= "<td style='font-weight: bold'><i class=\"fas fa-caret-right\"></i> ".$csClientes->NomeCompleto."</td>";
            $visualizar .= "<td style='text-align: center;'>R$ ".number_format($csClientes->ValorTotal,2,',','.')."</td>";
            $visualizar .= "<td style='text-align: center'><span class='status'><a href='#!' id='btnAlterar' data-id='".$csClientes->IdFinanceiro."' class='btn btn-danger btn-xs'>Pendente</a></span></td>";
            $visualizar .= "</tr>";
    }

No JQuery

<script>
$(document).on('click',"#btnAlterar", function(){
  var IdFinanceiro = $(this).attr('data-id');
    $.post('sistema/alterar-status.php', {key: IdFinanceiro}, function(retorno){
    if(retorno == 1){
      $(".status").html("<a href='#!' id='btnAlterar' data-id='<?php echo $csClientes->IdFinanceiro; ?>' class='btn btn-success btn-xs'>Pago</a>");
    }else{
      $(".status").html("<a href='#!' id='btnAlterar' data-id='<?php echo $csClientes->IdFinanceiro; ?>' class='btn btn-danger btn-xs'>Pendente</a>");
    }
    });
});
</script>

PHP that changes status

$sql = mysqli_query($this->conexao,"UPDATE pagamentos SET StatusPagamento = 'P' WHERE IdPagamento = '".$_POST["key"]."';");
if(mysqli_affected_rows($this->conexao) > 0){
  echo '1';
}else{
  echo '0';
}

When I click the Fernando Pessoa button, it changes not only his but also everyone's.

Itriedthiswaytoo:

<script>$(document).on('click',"#btnAlterar", function(){
      var IdFinanceiro = $(this).attr('data-id');
        $.post('sistema/alterar-status.php', {key: IdFinanceiro}, function(retorno){
        if(retorno == 1){
           $("#statusPago").css('display','block');                             
           $("#statusAberto").css('display','none');
        }else{
           $("#statusAberto").css('display','block');
           $("#statusPago").css('display','none');
        }
        });
    });
    </script>

And in HTML

   <td style='text-align: center'>
        <span id='statusAberto' style='display: block'><a href='#!' id='btnAlterar' data-id="<?php echo $csClientes->IdFinanceiro; ?>" class='btn btn-danger btn-xs'>Pendente</a></span>
        <span id='statusPago' style='display: none'><a href='#!' id='btnAlterar' data-id="<?php echo $csClientes->IdFinanceiro; ?>" class='btn btn-success btn-xs'>Pago</a></span>
  </td>

But this time it changes the first and not the Fernando Pessoa when I click:

    
asked by anonymous 12.12.2018 / 11:11

1 answer

1

Your problem is on line $(".status").html("<a href='#!' id='btnAlterar' data-id='<?php echo $csClientes->IdFinanceiro; ?>' class='btn btn-success btn-xs'>Pago</a>");

In this line you add the html in all elements of the page that have the class .status , to bypass this you need to identify the <a> that called the function and add the class in .status in the parent: $(that).parent(".status")

Remembering that you must declare a variable out of $.post() so that this can be accessible within the callback: var that = this;

$(document).on('click',"#btnAlterar", function(){
  var that = this;
  var IdFinanceiro = $(this).attr('data-id');
    $.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>");
    }else{
      $(that).parent(".status").html("<a href='#!' id='btnAlterar' data-id='<?php echo $csClientes->IdFinanceiro; ?>' class='btn btn-danger btn-xs'>Pendente</a>");
    }
    });
});
    
12.12.2018 / 12:51