Partage
  • Partager sur Facebook
  • Partager sur Twitter

somme input avec id

Sujet résolu
    11 octobre 2018 à 14:07:35

    bonjour à tous,

    après avoir résolu un problème , en voila un autre .....

    j'ai donc une formulaire avec une fonction calcul JS.

    je cherche à additionner tous les resultats prix_total  avec une fonction somme JS.

    la problèmatique c'est que je cherche à additionner des input qui ont le  même nom ( prix_total ) avec un id différent (prix_total<?php echo ($result->Id_produit);?> ).

    je précise que la somme peut être avec 2 input ou 10 , c'est aléatoire en fonction des produits vendu ...

    voila mon code

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Accueil</title>
    <script>
    function Calcul(id) {
    console.log("toto");
    Prix = document.getElementById('prix_total'+id).value;
    Quantite = document.getElementById('quantite'+id).value;
    var Prixtot = Prix*Quantite;
    document.getElementById('prix_total'+id).value= Prixtot;
    }
    </script> 
    
    
    
    </head>
    
    <body>
    <div class="tableau">
    <table style="border-collapse: collapse;">
    <thead>
    <tr>
    <th>Désignation</th>
    <th>Modèle</th>
    <th>Prix</th>
    <th>Quantité</th>
    <th>Total</th>
    <th>Règlement</th>
    </tr>
    </thead>
    <tbody>
    
    
    <?php
    
    include('connect.php');
    
    $resultat=$pdo->query("SELECT * FROM Produit ORDER BY Id_produit ASC");
    $resultat->setFetchMode(PDO::FETCH_OBJ);
    while( $result = $resultat->fetch() )
    {
    
    ?>
    
    
    <tr>
    <td><input type="text" id="designation_produit" name="designation_produit" value="<?php echo ($result->designation_produit);?>"></td>
    <td><input type="text" id="modele_produit" name="modele_produit" value="<?php echo ($result->modele_produit);?>"></td>
    <td><input type="text" id="prix<?php echo ($result->Id_produit);?>" name="prix_produit" value="<?php echo ($result->prix_produit);?>"></td>
    <td><input type="text" id="quantite<?php echo ($result->Id_produit);?>" name="quantite" value="" onBlur="Calcul(<?php echo ($result->Id_produit);?>)"></td>
    <td><input type="text" id="prix_total<?php echo ($result->Id_produit);?>" name="prix_total" value=""></td>
    <td><select id="reglement_produit" name="reglement_produit">
    <option selected="selected">Choisir un règlement</option>
    <?php
    
    $resultats=$pdo->query("SELECT * FROM Reglement ORDER BY Id_reglement");
    $resultats->setFetchMode(PDO::FETCH_OBJ);
    while( $result_reglement = $resultats->fetch() )
    
    {
    	echo '<option value="'.$result_reglement->Id_reglement.'">'.$result_reglement->reglement.'</option></br>';
    }
    
    ?>
    </select></td>
    </tr>
    
    
    <?php
    
    }
    ?>
    
    </tbody>
    </table>
    </div>
    
    </body>
    </html>


    merci pour votre aide

    c'est bon j'ai trouvé ......

    merci à vous et je mets le code au cas ou .....à adapter selon vos besoins

    <!DOCTYPE html>
         <form>
              <p>Fee 1: <input class="input_small_fee" value="" name="fee1" maxlength=5>
              <p>Fee 2: <input class="input_small_fee" value="" name="fee2" maxlength=5>
              <p>Fee 3: <input class="input_small_fee" value="" name="fee3" maxlength=5>
              <p>TOTAL : <span id="total" style="color:red;"></span>
         </form>
     
    <script type="text/javascript">
         function addition() {
              result = 0;
              for (j=0; j<array.length; j++) {
                   if ( array[j].value.length > 0 ) {
                   if ( !/^\d+(\.\d{1,2})?$/.test(array[j].value) )
                        array[j].style.backgroundColor = "whitesmoke" ;
                   else {
                        array[j].style.backgroundColor = "white";
                        result = parseFloat(result) + parseFloat(array[j].value);
                        }
                   }                   
                   }
              total.innerHTML = result.toString();
              }
         array = document.getElementsById('prix_total'+id) ;
         for (j=0; j<array.length; j++) { array[j].addEventListener("blur", addition) }
         document.forms[0].elements['prix_total1'].focus();
    </script>

    c'est bon j'ai trouvé ......

    merci à vous et je mets le code au cas ou .....à adapter selon vos besoins

    <!DOCTYPE html>
         <form>
              <p>Fee 1: <input class="input_small_fee" value="" name="fee1" maxlength=5>
              <p>Fee 2: <input class="input_small_fee" value="" name="fee2" maxlength=5>
              <p>Fee 3: <input class="input_small_fee" value="" name="fee3" maxlength=5>
              <p>TOTAL : <span id="total" style="color:red;"></span>
         </form>
     
    <script type="text/javascript">
         function addition() {
              result = 0;
              for (j=0; j<array.length; j++) {
                   if ( array[j].value.length > 0 ) {
                   if ( !/^\d+(\.\d{1,2})?$/.test(array[j].value) )
                        array[j].style.backgroundColor = "whitesmoke" ;
                   else {
                        array[j].style.backgroundColor = "white";
                        result = parseFloat(result) + parseFloat(array[j].value);
                        }
                   }                   
                   }
              total.innerHTML = result.toString();
              }
         array = document.getElementsById('prix_total'+id) ;
         for (j=0; j<array.length; j++) { array[j].addEventListener("blur", addition) }
         document.forms[0].elements['prix_total1'].focus();
    </script>

    -
    Edité par laurentgrosges 11 octobre 2018 à 14:54:30

    • Partager sur Facebook
    • Partager sur Twitter

    somme input avec id

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown