Skip to content Skip to sidebar Skip to footer

Using Javascript Function In Php While Loop

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

Solution 1:

<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.


Solution 2:

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


Post a Comment for "Using Javascript Function In Php While Loop"