2016-09-20 20 views
-1

私のプロジェクトでは、線の小計(quantite * montant)をダイナミックにする必要があります。私が持っている瞬間のためにPHPの動的小計

enter image description here

私はそれが自動でmontantの合計をしたいが、たとえば1 * 110を作り、それが直接表示されます。

マイコード:

<div id="contenu"> 
    <h2>Renseigner ma fiche de frais du mois <?php echo $numMois."-".$numAnnee ?></h2> 

    <form method="POST" action="index.php?uc=gererFrais&action=validerMajFraisForfait"> 
    <div class="corpsForm"> 

     <fieldset> 
     <legend>Eléments forfaitisés 
     </legend> 
<table width=100%> 
     <tr> 
     <td>Libelle</td> 
     <td>Nombre</td> 
     <td>Montant unitaire</td> 
     <td>Montant total</td> 
    </tr> 
     <?php 
      foreach ($lesFraisForfait as $unFrais) 
      { 
       $idFrais = $unFrais['idfrais']; 
       $libelle = $unFrais['libelle']; 
       $quantite = $unFrais['quantite']; 
       $montant = $unFrais['montant'];     
     ?> 
    <tr> 
     <td><?php echo $libelle ?></td> 
     <td><input type="number" id="idFrais" name="lesFrais[<?php echo $idFrais?>]" size="10" min="0" autocomplete="off" maxlength="5" value="<?php echo $quantite?>" onkeyup="calculer()"> 
     <td><input type="text" id="montant" value="<?php echo $montant ?>" disabled></td> 
    </tr>   
     <?php 
      } 
     ?> 
    </table>   
     </fieldset> 
    </div> 
    <div class="piedForm"> 
    <p> 
    <input id="ok" type="submit" value="Valider" size="20" /> 
    <input id="annuler" type="reset" value="Effacer" size="20" /> 
    </p> 
    </div>   
    </form> 
+0

UはそれをJqueryによって行うことができます。 – harry

答えて

0

ここでもう1つの回答を追加しました。

だから基本的に、あなたのForeachループでは、あなたはすでにこの下に、あなたのjavascriptのコードのonkeyupのは= 'calculer' を持っているのでINCREMENTAL VARIABLE

<?php 
      $incr = 1; 
      foreach ($lesFraisForfait as $unFrais) 
      { 
       $idFrais = $unFrais['idfrais']; 
       $libelle = $unFrais['libelle']; 
       $quantite = $unFrais['quantite']; 
       $montant = $unFrais['montant']; 

     ?> 
    <tr> 
     <td><?php echo $libelle ?></td> 
     <td><input type="number" id="<?php echo 'idFrais'.$incr; ?>" size="10" min="0" autocomplete="off" maxlength="5" value="<?php echo $quantite?>" onkeyup="calculer(this)"> 
     <td><input type="text" id="<?php echo 'montant'.$incr; ?>" value="<?php echo $montant ?>" disabled></td> 
<td id='subtotal<?php echo $incr;?>'><?php echo $quantite*$montant; ?></td> 
    </tr> 

     <?php 
       $incr ++; 
      } 
     ?> 

を作成する必要があります。

function calculer(e){ 

var i = e.getAttribute('id').length; 
var input_identifier = e.getAttribute('id').substring(i-1,i); 
var subtotal = document.getElementById('subtotal'+input_identifier) 
var quantity = e.value; 
var montant = document.getElementById('montant'+input_identifier); 

subtotal.textContent = parseInt(quantity) + parseInt(montant.value); 

return; 

} 

これは動作するはずです。

+0

いいえ:(私がモンタントトータルパッサチェンジの指示を変更したとき:http://image.noelshack.com/fichiers/2016/38/1474370109-screenshot-20160920131435.png –

+0

私はこれを試しています:function Calcule() { VAR C1 =のdocument.getElementById( 'idFrais')値; するvar C2 =のdocument.getElementById( 'montant')値; VARのTT = C1の* C2を のdocument.getElementById( '総')のinnerHTML =。 TT; } –

+0

そして:\t \t \t​​の \t \t \t​​の \t \t \t​​

0

合計TDを追加してください:

<tr> 
      <td><?php echo $libelle ?></td> 
      <td><input type="number" id="idFrais" name="lesFrais[<?php echo $idFrais?>]" size="10" min="0" autocomplete="off" maxlength="5" value="<?php echo $quantite?>" onkeyup="calculer()"> 
      <td><input type="text" id="montant" value="<?php echo $montant ?>" disabled></td> 


    <td><input type="text" value="<?php echo ($montant*$quantite) ?>" disabled></td> 
     </tr> 
+0

私は言いましたが、私はどこでナンバー2をモンタントトータルチェンジするのか、ナンバートを直接送るのはいいのですか?いいえ、私はformulaireを送るときはいいえ –

0

は、あなただけの、次の4番目のTDを追加する必要があります。

<td><?php echo $quantite*$montant; ?></td> 

これでうまくいくはずです。

+0

私はそれがうまくいくことをよく知っています。人が数量を変更するとすぐに結果が転記されます。 –

+0

PHPでこれを行うことはできません... JAVASCRIPTを使用して行う必要があります。あなたのポストは "PHPの動的小計" – victor

+0

あなたがダイナミックな "ユーザーインタラクション"をしたい場合は..あなたはJavascriptのPHPでそれを行う必要があります。 これを行うには、document.getElementById( 'idFrais')でJavascriptでListenerを作成する必要があります。addEventListener( 'keypress'、function(){}); – victor