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

your function works fine for me. Is there a link you can provide to see it in action?

You could try putting in alert('first'); as the first line of your function and then alert('last'); as the last line of your function to see if they are getting called without a problem. Also, put alert(<?php echo $myvar; ?>); in your function if those work to see if it reports a value for $myvar. If the prompt comes up but is blank or something other than the expected value of $myvar then you know the issue is with getting $myvar set correctly.

try this:
Lets assume $myvar variable is having values like:

$myvar='8','9','10';

and in your java script:

jsvar =new Array(<?php echo $myvar;?>);

then you can run a loop through java script variable like"

for(var k=0;k<jsvar.length;k++)
{
alert(jsvar[k]);
}

Thanks a lot for your valuable replies SolidSolutions & Shanti Chepuru.
Yes I have uploaded my file to:
http://believecreation.org/atest/
Sorry for the botheration.
My overall code so far is:

<?PHP
$number=$_POST['number'];
$name=$_POST['name'];
$radio=$_POST['radio'];
$date=$_POST['date'];
$sales_person=$_POST['sales_person'];
$job=$_POST['job'];
$payment_terms=$_POST['terms'];
$due_date=$_POST['due_date'];
$subtotal=$_POST['subtotal'];
$tax=$_POST['tax'];
$advance=$_POST['advance'];
$total=$_POST['total'];
$work=$_POST['work'];
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">
 function line(elem) { 
var jsvar = "<?php echo $myvar; ?>";
 document.getElementById("line_total").value = jsvar;        
 alert('first');
 alert('last');
 alert(<?php echo $myvar; ?>);      
 }
 
 </script>

			<?PHP
			$sql=mysql_query
			("INSERT INTO test 
			(inv_number,
			qty,
			description,
			unit_price,
			line_total) 
    		VALUES
			('".$number."',
			'".$quantity."',
			'".$description."',
			'".$unit_price."',
			'".$line_total."')");
	}		
} 
<html>
<head>



<script type="text/javascript">
function tot(elem) {
var d=document.getElementById("subtotal").value;
var st=Number(d); 
var e=document.getElementById("tax").value;
var tx=Number(e); 
var f=document.getElementById("advance").value;
var ad=Number(f); 
var g=(st+tx)-ad;
document.getElementById("total").value = g;
}
</script>
<script type="text/javascript">
        function addRow(tableID) {
 
            var table = document.getElementById(tableID);
 
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
 
            var colCount = table.rows[0].cells.length;
 
            for(var i=0; i<colCount; i++) {
 
                var newcell = row.insertCell(i);
 
                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }
</script>
<body>
<fieldset>
<legend>Invoice Details</legend>
<form name="newquotation1" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<table align="center">
<tr>
<td align="center" colspan="2"><span class="style2">All Fields Are Mandatory.</span></td>
</tr>
<tr>
<?
/* onSubmit="return validateForm(this)"
*/
/*$query=mysql_query
("SELECT * FROM existing_clients_invoice");
if(mysql_num_rows($query) > 0)
{
while($row=mysql_fetch_array($query))
{
if($row!=0)
{
$id=$row['inv_id']+1;
}
}
}
else
{
$id=1;
}
$ini="IN";
$year=date("y");
$invoice= strtoupper($ini) . '-'  .($year) . '-' . '00' .$id;
*/?>
<td>Invoice Number:</td>
<td><input type="text" name="number" readonly"" value="<?PHP /*echo $invoice;*/ ?>"></td>
</tr>
<tr>
<td>Client Name:</td>
<td>
<?PHP
/*$query=mysql_query("SELECT * FROM client_master");
echo "<select name='name'>";
while($row=mysql_fetch_array($query))
{
echo "<option value=$row[client_id]>
$row[client_name]</option>";
}
echo "</select>";
*/?>
</td>
</tr>
<tr>
<td>Work Order Number:</td>
<td width="300">Applicable&nbsp;<input name="radio" type="radio" value="applicable">&nbsp;
<?PHP
/*$query=mysql_query("SELECT * FROM existing_clients");
echo "<select name='work'>";
while($row=mysql_fetch_array($query))
{
echo "<option value=$row[exi_work_number]>
$row[exi_work_number]</option>";
}
echo "</select>";
*/?>
&nbsp;Not Applicable&nbsp;<input name="radio" type="radio" value="notapplicable" >
</td>
</tr>
<tr>
<td>Invoice Date:</td>
<td><input id="demo3" name="date" type="text" >
<a href="javascript:NewCssCal('demo3','yyyymmdd')">
<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a></td>
</tr>
<tr>
<td>Sales Person:</td>
<td><input type="text" name="sales_person"></td>
</tr>
<tr>
<td>Job:</td>
<td><input type="text" name="job"></td>
</tr>
<tr>
<td>Payment Terms:</td>
<td><select name="terms">
  <option value="Cheque" selected>Cheque</option>
  <option value="Cash">Cash</option>
</select></td>
</tr>
<tr>
<td>Due Date:</td>
<td><input id="demo4" name="due_date" type="text" >
<a href="javascript:NewCssCal('demo4','yyyymmdd')">
<img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a></td>
</tr>
</table>
<tr>
<td><input type="button" value="Add Row" onClick="addRow('dataTable')" ></td>
<table align="center" id="dataTable" width="912">
<tr>
<td width="68">Quantity:</td>
<td width="144"><input type="text" name="qty[]" id="qty"></td>
<td width="77">Description:</td>
<td width="144"><input type="text" name="description[]" id="description"></td>
<td width="80">Unit Price:</td>
<td width="144"><input type="text" name="unit_price[]" id="unit_price"></td>
<td width="75">Line Total:</td>
<td width="144"><input type="text" name="line_total[]" id="line_total" onBlur="return line(this)"></td>
</tr>
</table>
<table align="center" width="913">
<tr>
<td width="67">Subtotal:</td>
<td width="146"><input type="text" name="subtotal" id="subtotal" onBlur="return lt(this)"></td>
<td width="76">Taxes:</td>
<td width="144"><input type="text" name="tax" id="tax"></td>
<td width="80">Advance:</td>
<td width="144"><input type="text" name="advance" id="advance"></td>
<td width="76">Total:</td>
<td width="144"><input type="text" name="total" id="total" onBlur="return tot(this)" ></td>
</tr>
</table>
<table align="center">
<tr>
<td><input name="submit" type="submit" value="Submit"></td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>

Thanks in anticipation.

As you can see,nothing is alerted there and it is not responding onblur event.

Finally I got solution for this problem after a long search over internet and forum :)
And I am thankful to all you guys for ur constant support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.