Hi!!
I am trying to find out solution for this since long.
I tried js but as I am not good with it, I just want to do this through PHP.
I am adding dynamic rows when user clicks the Add Row button.I want to show the calculated amount like:
line_total=qty*unit_price;
if (isset($_POST['qty']) && sizeof($_POST['qty']) > 0) {
for($i = 0, $maxi = count($_POST['qty']); $i < $maxi; $i++) {
$quantity = (isset($_POST['qty'][$i]) && !empty($_POST['qty'][$i])) ? mysql_real_escape_string($_POST['qty'][$i]) : 0;
$description = (isset($_POST['description'][$i]) && !empty($_POST['description'][$i])) ? mysql_real_escape_string($_POST['description'][$i]) : 0;
$unit_price = (isset($_POST['unit_price'][$i]) && !empty($_POST['unit_price'][$i])) ? mysql_real_escape_string($_POST['unit_price'][$i]) : 0;
$line_total = (isset($_POST['line_total'][$i]) && !empty($_POST['line_total'][$i])) ? mysql_real_escape_string($_POST['line_total'][$i]) : 0;
?>
<?php $myvar=$quantity*$unit_price; ?>
<script type="text/javascript">
jsvar = <?php echo $myvar; ?>;
document.write(jsvar); // Test to see if its prints array:
</script>
}
}
This code works very well and displays the values.But if I use it in the function like;
function line(elem) {
jsvar = <?php echo $myvar; ?>;
document.getElementById("line_total").value = jsvar;
}
And call it:
<input type="text" name="line_total[]" id="line_total" onBlur="return line(this)">
It is not showing the result. Kindly guide me where I am going wrong