Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
Dynamicly update php variable on change of multiple input fields without page reload

I'm not familiar with js and tried the example shown here (jQuery - update variable when input values changed) but it's not working for me.

What I need is the following: I have a variable, that has to show the total qty entered in input fields. I.e on every input field change I have to sum the entered qty. My code is as follow:

<?PHP
$counter = 0;
?>

<div class="total">
    TotalPcs:
    <span class="running_total">
        <?= $counter ?> <!-- this is the var showing the total qty -->
    </span>
</div>
<?php
<form method="post">
    foreach ($object->getExternalProducts($orderlang) as $result) {
        print '<tr>';
        print '<td>' . $result->label . '</td>';
        print '<td><input type="number" class="qty_field" name="productid_' .$result->rowid . '" value="" /></td>';
        print '<tr>';
    }
</form>
?>

As you can see, the name of the field is populated based on SQL query and appends the ID of each product after productid_ so I cannot use the name field. I can strip the 'productid_' but it's obvious that it is easier to use 'class' or 'id' of the field.

And the JS file:

$('body').on('keyup', '.qty_field', function () {
    var sum = 0;
    $('.qty_field').each(function() {
        sum += Number($(this).val());
    });

    $('.running_total').html(sum);
});

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda