using javascript function in php while loop

前端 未结 2 1002
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 08:12

I am trying to multiply values in two text boxes and place it on a third text box... Using javascript and php while loop with onclick event.....How to do that?? my code is

相关标签:
2条回答
  • 2021-01-29 08:53
    <script language="javascript">
    
        function multiply(arg_id)
        {
    
           var textValue1 = document.getElementById('CLS'+arg_id).value;
           var textValue2 = document.getElementById('rate'+arg_id).value;
    
           document.getElementById('valuation'+arg_id).value = textValue1 * textValue2;
    
        }
        </script>
    <?php
    $a="some query";
    $b=mysql_query($a);
    while($c=mysql_fetch_array($b))
    
    {
    echo "<td>".$CLOSINGstk."</td>
    <input type='hidden' name='CLOSINGstk".$a."' value='".$CLOSINGstk."' id='CLS".$a."'>";
    echo "<td><input type='text' name='rate".$a."' id='rate".$a."'></td>";
    echo "<td><input type='text' name='valuation".$a."' id='valuation".$a."' onclick='multiply(".$a.");'></td>";
    }
    
    ?>
    

    just pass parameter as id from all CLS,RATE,VALUATION

    let me know i can help you more.

    0 讨论(0)
  • 2021-01-29 09:03

    Try like this

    while($c=mysql_fetch_array($b)) {
    echo "<td>
              <input type='text' name='valuation".$c['id']."'
               id='valuation".$c['id']."' onkeypress='multiply(".$c['id'].");'>  //Let you want to pass id from DB
          </td>";
    

    or you can use

    onkeypress='multiply('.$c['id'].')'
    

    USE onkeypress instead of onclick() and in your script

    function multiply(id)
    {
    
       var textValue1 = document.getElementById('CLS'+id).value;
       var textValue2 = document.getElementById('rate'+id).value;
    
       document.getElementById('valuation'+id).value = textValue1 * textValue2;
    
    }
    

    And try to avoid mysql_* functions because they are depricated,instead use either mysqli_* functions or PDO statements

    0 讨论(0)
提交回复
热议问题