Home » Php » problem to recover the value of an input [javascript, php]

problem to recover the value of an input [javascript, php]

Posted by: admin February 25, 2020 Leave a comment

Questions:

The employee must not exceed 100€ of product, so I wanted him to write down the quantity of product he wants, press a button and a dialog box appears to display the total price of the order.
I managed to have the price of the item displayed for each product in the dialog box and to have the prices for each chosen product added together.

However, I cannot recover the value of the input which is the quantity chosen by the employee.

    $lenum = 0;
      foreach ($lesProduits as $leProduit){
        ?>
        <img src="<?php echo $leProduit['image'];?>">
        <br><input type="checkbox" id="<?php echo $leProduit['nproduit']?>" name="nproduits[]" value="<?php echo $leProduit['nproduit']?>">
        <?php echo $leProduit['nproduit']; ?> - <?php echo $leProduit['designation']; ?><br>
        Boitage : <?php echo $leProduit['boitage'];?><br>
        Prix Unitaire : <?php echo $leProduit['prix']; ?>€<br>
        Quantité : <?php echo $leProduit['qtestock'];  ?> <br>
        Quantité souhaité :  <input type="text" name="txtqte[]" id="<?php echo $lenum;?>"  size="3"/><br>
        <input type="button" value="Enregistrer" onclick="leprix(lep = <?php echo $leProduit['prix'];?>,<?php echo $lenum;?>)">
        <br>
        <hr><br>
        <?php
        $lenum ++;
      }
      ?> 

and the script

function leprix(lep,inputId){
    var lavaleur = document.getElementById(inputId).value;
    alert("la prix du produit est de : " + lep);
    alert("la quantité demandé est de : " + lavaleur);
}

For the first product when $lenum = 0 it works but then it displays $lenum and not the value of the input

How to&Answers:

This should work:

   <input type="text" name="txtqte[]" name="<?php echo $lenum;?>" id="<?php echo $lenum;?>"  size="3"/>
   <input type="button" value="Enregistrer" onclick="leprix('<?php echo $lenum;?>')">
   <script>
       function leprix(inputId){
            var input = document.getElementById(inputId).value;
            alert(input);
       }
   </script>