SUM dynamic input field onchange with javascript - javascript

how to sum all those field (name="envelope[]") and display the result in another input. I'm not sure how to do that because those fields are generated dynamycally. tks Seby
UPDATE: here's what I tried with the javascript function. the problem I have is that the function start but gives me the result of all envelope[] input. But i need the result on every lines, not from all the envelope[] fields.
I've change the hilighted field, but the top amount changed.
see example here: http://www.soniajanelle.ca/example.jpg
function findTotal(){
var arr = document.getElementsByName('envelope[]');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementById('totalsum').value = tot;
}
</script>
</head>
<body>
<div id="slimScroll" style="overflow:auto; overflow-x:hidden; overflow-y:hidden; height:<? echo $row_admin_detail['size_height'];?>px;" >
<table width="100%" align="center" border="0">
<tr><td height="0px" align="center" colspan="2"></td></tr>
<tr>
<table border="0" width="100%" class="ft12" >
<tr><td class="ann_title" align="center" colspan="2"><h2>Transactions</h2></td></tr>
<td valign="top" align="center" >
<div id="delDiv" >
</table>
<table width="450px" border="0" cellspacing="0" cellpadding="0" >
<tr class="table" height="125px">
<th width="1%" align="center"></th>
<th width="3%" align="center"></th>
<th width="10%" align="center" colspan="2" >Action</th>
<th width="8%" align="center">Type</th>
<th width="8%" align="center">Date</th>
<th width="20%" align="center">Payee</th>
<th width="8%" align="center">Amount</th>
<th width="20%" align="center">Envelope</th>
<th width="2%" align="center"></th>
<?
$query = "SELECT * from envelope_sub order by budgeted_amount desc";
$result = mysql_query($query) or die(mysql_error());
while ($envelopeSub = mysql_fetch_array($result))
{
echo "
<th width=\"8%\" align=\"right\"
><p><FONT size=\"2\"> ".$envelopeSub['real_amount']=number_format($envelopeSub['real_amount'],2,'.','')."$</FONT> </p><br>
<p class=\"rotate\">".$envelopeSub['name']."</p></th>
";
}
?>
</tr>
<tr >
<form method="post" action="main.php?act=stats&do=add&type=insert">
<th align="center"></th>
<th align="center"><img src="http://www.sebyphotographe.com/simplestudio/images/plus-icon 2.png" class="removeimg"></a></th>
<th align="center" colspan="2" ><div class="ann_search_submit"><input class="ann_submit" type="submit" value="Ajouter" /></div></th>
<th align="center"><input class="ann_textbox" name="type" type="text" size="8%"/></th>
<th align="center"><input class="ann_textbox" name="date" type="text" size="8%"/></th>
<th align="center"><input class="ann_textbox" name="payee" type="text" size="20%"/></th>
<th align="center"><input class="ann_textbox" name="amount" type="text" size="8%" onkeypress="return isNumeric(event)" /></th>
<th align="center"><select>
<option selected="selected">Distribuer montant...</option>
<option>par pourcentage</option>
<option>par montant fixe</option>
<option>Manuellement</option>
</select></th>
<th align="center"></th>
<?
$query = "SELECT * from envelope_sub order by id desc";
$result = mysql_query($query) or die(mysql_error());
while ($envelopeSub = mysql_fetch_array($result))
{
echo "
<input type=\"hidden\" name=\"envelope_id[]\" value=\"".$envelopeSub['id']."\">
<th align=\"center\"><input class=\"ann_textbox\" name=\"envelope[]\" type=\"text\" size=\"8%\" onkeypress=\"return isNumeric(event)\" /></th>
";
}
?>
</form>
</tr>
<?
$i=0;
$k=0;
$transactions = mysql_query("select * from transactions");
while($row_transactions=mysql_fetch_array($transactions))
{
?>
<form method="post" action="main.php?act=stats&do=edit&type=update">
<input type="hidden" name="id" value="<? echo $row_transactions['id']?>">
<tr class="<?=$bgcolor?>" nowrap="nowrap" onmouseover="style.backgroundColor='#EAEFF3';" onmouseout="style.backgroundColor='<?=$bgs?>'" >
<td align="left" nowrap="nowrap"> </td>
<td colspan="2" ><img src="http://www.sebyphotographe.com/simplestudio/images/icon_delete.png" class="removeimg"></td>
<td ><div class="ann_search_submit"><input class="ann_submit" type="submit" value="Update" ></div></td>
<td ><input class="ann_textbox" name="type" type="text" value="<?echo $row_transactions['type']?>" size="8%"/></td>
<td ><input class="ann_textbox" name="date" type="text" value="<?echo $row_transactions['date']?>" size="8%"/></td>
<td ><input class="ann_textbox" name="payee" type="text" value="<?echo $row_transactions['payee']?>" size="20%"/></td>
<? $row_transactions['amount']=number_format($row_transactions['amount'],2,'.',''); ?>
<td >
<? if ($row_transactions['type']=="Expense"){?>
<input id="totalsum" class="ann_textbox" name="amount" type="text" style="color:red;text-align:right;" value=" <?echo "(".$row_transactions['amount'].")";?>" size="8%"/></td><?
}ELSE{ ?>
<input id="totalsum" class="ann_textbox" name="amount" type="text" style="text-align:right;" value=" <?echo $row_transactions['amount'];?>" size="8%"/></td><? } ?>
<td ><input class="ann_textbox" name="envelope_<?echo $i?>" type="text" value="<?echo $row_transactions['envelope']?>" /></td>
<td></td>
<?
$query_env = "SELECT * from transactions_details WHERE transactions_id='".$row_transactions['id']."' order by enveloppe_sub_id desc";
$result_env = mysql_query($query_env) or die(mysql_error());
while ($env_amount = mysql_fetch_array($result_env))
{
if ($env_amount['amount']==0){$env_amount['amount']="";}
$env_amount['amount']=number_format($env_amount['amount'],2,'.','');
if ($env_amount['type']=="Expense"){
echo "
<input type=\"hidden\" name=\"trans_id[]\" value=\"".$env_amount['transactions_id']." \">
<input type=\"hidden\" name=\"envelope_id[]\" value=\"".$env_amount['enveloppe_sub_id']."\">
<input type=\"hidden\" name=\"type_env[]\" value=\"".$row_transactions['type']."\">
<td align=\"center\"><input onchange=\"findTotal()\" class=\"ann_textbox\" name=\"envelope[]\" type=\"text\" size=\"8%\" style=\"text-align:right;color:red\" value=\"(".$env_amount['amount'].")\" /></td>";
}ELSE{
echo "
<input type=\"hidden\" name=\"trans_id[]\" value=\"".$env_amount['transactions_id']." \">
<input type=\"hidden\" name=\"envelope_id[]\" value=\"".$env_amount['enveloppe_sub_id']."\">
<input type=\"hidden\" name=\"type_env[]\" value=\"".$row_transactions['type']."\">
<td align=\"center\"><input onchange=\"findTotal()\" class=\"ann_textbox\" name=\"envelope[]\" type=\"text\" size=\"8%\" style=\"text-align:right;\" value=\"".$env_amount['amount']."\" /></td>";
}
}
?>
</tr>
</form>
<?
$i++;
$k++;
}
?>

Something like this should do the trick
var elems = document.getElementByName('envelope[]');
var sum = 0;
for (var i = 0; i < elems.length; i++)
{
sum += parseInt(elems[i].value);
}
document.getElementById('yourelementid').value = sum;

Related

trying to allow an empty row to the bottom of a input table using JavaScript

I am trying to create a form where the user can click on a button that will create a blank row at the bottom of the table. They can click this button for as many rows as they want to add. For some reason, when I click on the button I created, it goes back to my index.php page instead of just adding the row to the current page. Here is some of the code. Please let me know if there is more information that I should be adding here.
Thank you
JavaScript:
function new_line() {
var t = document.getElementById("desc_table");
var rows = t.getElementsByTagName("tr");
var r = rows[rows.length - 1];
var x = rows[1].cloneNode(true);
x.style.display = "";
r.parentNode.insertBefore(x, r);
}
<?php include 'connect.php'; ?>
<!DOCTYPE xhtml>
<html>
<head>
<style type="text/css"><?php //include'css\file.css';?></style>
<script type="text/javascript" src="js/new_line.js"></script>
<script type="text/javascript" src="js/menu.js"></script>
<body>
<button id="home" onclick="home_btn()">Home</button>
<title>New Estimate</title>
<h1>New Estimate</h1>
<?php
$sql = $conn->prepare('SELECT * FROM entire_info');
$sql -> execute();
$result = $sql ->get_result();
if ($result->num_rows!=0) {
$entire_info=$result->fetch_assoc();
}
else{
echo'Cannot find company information<br>';
}
?>
<form>
<table id="estimate_table">
<tr>
<td>
<?php
echo '<h1>'.$entire_info['name'].'</h1><br>'.
$entire_info['str_address'].'<br>'.
$entire_info['city'].', '.$entire_info['province'].' '.$entire_info['postal']
.'<br>Phone:'.$entire_info['phone'].'<br>Fax:'.$entire_info['fax'].'<br>'.$entire_info['email'].'<br>';
?>
<br>
</td>
<td colspan="3" style="text-align: right;"><?php echo '<h1>Estimate</h1>'; ?></td>
</tr>
<tr>
<td>
<table>
<tr>
<td style="vertical-align: top;">
For:<br>
<select name="estimate_for">
<?php
$sql = $conn->prepare('SELECT * FROM company');
$sql -> execute();
$result = $sql ->get_result();
if ($result->num_rows>0) {
while ($row=$result->fetch_assoc()) {
$company_name = $row['company'];
echo '<option value="'.$company_name.'">'.$company_name.'</option>';
}
}
else{
echo'Cannot find company information<br>';
}
?>
</select>
</td>
<td>
Job:<br> <textarea name="job_name" style="width: 200px ! important;" style="height: 30px ! important;"></textarea>
</td>
</tr>
</table>
</td>
<td colspan="3" style="text-align: right;">
Invoice#: <br>
Date: <input type="text" name="estimate_date" value="<?php echo date('M d, Y'); ?>">
</td>
</tr>
</table>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<table id="desc_table">
<tr>
<td><font><br><h3>Description</h3></font></td>
<td><font><h3>Hours</h3></font></td>
<td><font><h3>Rate</h3></font></td>
<td><font><h3>Amount</h3></font></td>
</tr>
<tr>
<td ><textarea name="description" style="width: 400px" style="height: 100px ! important;"></textarea></td>
<td> <input type="text" name="hours"></td>
<td> <input type="text" name="rate"></td>
<td><input type="text" name="amount"></td>
<td>
<button onclick="new_line();">+</button>
</td>
</tr>
<tr>
<td colspan="3" style="text-align: right;"><h3>Subtotal</h3></td>
<td><input type="text" name="subtotal"></td>
</tr>
<tr>
<td colspan="3" style="text-align: right;"><h3>GST (#87221 2410)
</h3></td>
<td><input type="text" name="gst"></td>
</tr>
<tr>
<td colspan="3" style="text-align: right;"><h3>Total</h3></td>
<td><input type="text" name="subtotal"></td>
</tr>
</table>
</form>
</head>
</body>
</html>
Your button is inside a <form> so by default it will behave like a submit button.
You have to set its type to a normal button by adding the type attribute:
<button onclick="new_line();" type="button">+</button>

Enable/disable multiple textboxes

Code:
<?php
include('conect.php');
$result = mysqli_query($conn,"SELECT * FROM `op` WHERE `type` = 2 ;");
echo "<table class='table table-striped table-hover'id='datatables-example'>
<tr>
<td class='pure-table'><b>Title 1</b></td>
<td class='pure-table'><b>Title 2</b></td>
<td class='pure-table'><b>Check 1</b></td>
<td class='pure-table'><b>Title 3</b></td>
<td class='pure-table'><b>VCheck 2</b></td>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody data-link='row' class='rowlink'>
<tr>
<td>' . $row['Op'] . '</td>
<td> <input type='text' name='T2' class='form-control'>
<td style='text-align:center;'> <input type='checkbox' name='C1' id='C1' >
<td> <input type='text' name='T3' id='T3' class='form-control' disabled >
<td style='text-align:center;'> <input type='checkbox' name='C2'>
</tr>
</tbody>
}
</table>";
mysqli_close($conn);
?>
<script language ="JavaScript">
document.getElementById('C1').onchange = function() {
document.getElementById('T3').disabled = !this.checked;
};
</script>
I want to enable/disable multiple textboxes generated by the row number of input data.
The first row works fine but the other lines not.
What am I doing wrong?
None of your <td> tags are closed in the second row.

Auto add row in html

I have table like this:
<form name="frm_data_nasabah2" method="post" enctype="application/x-www-form-urlencoded" action="<?php echo $page_action;?>">
<table class="table table-bordered table-hover" id="tab_logic_usaha1" border="1" width="80%">
<thead>
<tr style="background: rgb(227,241,252)">
<th class="text-center" align="center">
No
</th>
<th class="text-center" align="center">
Nama Pemegang Saham
</th>
<th class="text-center" align="center">
%tase Saham
</th>
<th class="text-center" align="center" >
Icon
</th>
</tr>
</thead>
<tbody>
<?php
$loop = 1;
foreach($PEMEGANGSAHAM as $saham){ ?>
<tr id='addr<?php echo $loop; ?>_usaha1' >
<td>
<?php echo $loop;
?>
</td>
<td>
<input type="text" name='Nama_Pemegang_Saham[]' class="form-control" value="<?php echo $saham->NAMA_PEMEGANG_SAHAM; ?>" required/>
</td>
<td>
<input type="text" name='tase_Saham[]' value="<?php echo $saham->PER_SAHAM; ?>" class="form-control" required/>
</td>
<td>
<center>
<img src="http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/delete.png"
onclick="confirm_Delete_Usaha1(<?php echo $saham->ID_USAHA; ?>)" height="20" width="20" />
</center>
</td>
</tr>
<?php $inputSaham++;
$loop++;
$row1++;
?>
<?php } ?>
</tbody>
</table>
<br/>
<input name="submitTab" value="Submit" type="submit">
<input type="hidden" name="id_num" value="<?php echo $debiturId;?>" />
<input type="hidden" name="submit_segment" value="usaha_saham" />
<input type="hidden" name="rowNum" value="<?php echo $row1; ?>" />
</form>
and i have javascript like this:
var i=<?php echo $loop; ?>;
$("#add_row_usaha1").click(function(){
$('#addr'+(i)+'_usaha1').html("<td>"+ (i-1) +"</td><td><input name='Nama_Pemegang_Saham"+i+"' type='text' class='form-control input-md' required/> </td>"+
"<td><input name='tase_Saham"+i+"' type='text' class='form-control input-md' required/></td>"+"<td>"
//+"<center><img src='http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/edit.png' height='20' width='20'/> "
//+"<img src='http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/delete.png' height='20' width='20'/></center>"
+"<center><img src='http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/deleteRow.png' height='20' width='20' onclick='deleteRow("+i+")'/></center>"
+"</td><?php $row1++; ?>");
$('#tab_logic_usaha1').append('<tr id="addr'+(i+1)+'_usaha1"></tr>');
i++;
});
And another function:
function deleteRow(input){
if(input>1){
$("#addr"+input+"_usaha1").html('');
input--;
}
}
when i try to delete row 5, and add some rows again, the result like this:
The number of row is not like the index. How can i fix this?
function deleteRow(input){
alert('masuk');
if(input>1){
todelete=document.getElementById("addr"+input+"_usaha1");
todelete.parentNode.removeChild(todelete);
i--;
}
}
Another Fix:
Fix Append the Element and then add its innerHTML
var i=<?php echo $loop; ?>;
$("#add_row_usaha1").click(function(){
$('#tab_logic_usaha1').append('<tr id="addr'+(i+1)+'_usaha1"></tr>');
$('#addr'+(i)+'_usaha1').html("<td>"+ (i-1) +"</td><td><input name='Nama_Pemegang_Saham"+i+"' type='text' class='form-control input-md' required/> </td>"+
"<td><input name='tase_Saham"+i+"' type='text' class='form-control input-md' required/></td>"+"<td>"
//+"<center><img src='http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/edit.png' height='20' width='20'/> "
//+"<img src='http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/delete.png' height='20' width='20'/></center>"
+"<center><img src='http://brftst.bni.co.id/tap.dev/public/images/imagesManageDebitur/deleteRow.png' height='20' width='20' onclick='deleteRow("+i+")'/></center>"
+"</td><?php $row1++; ?>");
i++;
});

Adding fields in a form dynamically with codeigniter

I am adapting this guide in codeigniter: http://www.quirksmode.org/dom/domform.html, in order to create a form with dynamic fields, all the functionality is working good, however when I try to echo the data in my controller with:
echo '<pre>'.print_r($this->input->post(),TRUE).'</pre>';
it just print the last group of values that were added in the view.
i.e.
Here is the js function:
var counter = 0;
function init() {
document.getElementById('moreFields').onclick = moreFields;
}
function moreFields() {
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
Here is the form code:
<?php echo form_open('admin/grabar_ruta'); ?>
<table width="100%">
<tr>
<td width="12%" align="right">Fuente:</td>
<td width="13%" align="left"><select name="fuente">
<?php foreach($fuentes as $row) { echo '<option value="'.$row->id.'">'.$row->serial.'</option>'; } ?> </select></td>
<td width="12%" align="right">Vehículo:</td>
<td width="13%" align="left"><select name="vehiculo">
<?php foreach($vehiculos as $row) { echo '<option value="'.$row->id.'">'.$row->placa.'</option>'; } ?> </select></td>
<td width="12%" align="right">Conductor:</td>
<td width="13%" align="left"><select name="conductor">
<?php foreach($conductores as $row) { echo '<option value="'.$row->id.'">'.$row->nombre.'</option>'; } ?> </select></td>
<td width="12%" align="right">Fecha:</td>
<td width="13%" align="left"><input type="date" name="name[]"></td>
</tr>
</table>
<div id="readroot" >
<table width="100%">
<tr>
<td width="12%" align="right">Origen:</td>
<td width="13%" align="left"><input type="text" name="origen" size="12"></td>
<td width="12%" align="right">Hora Salida:</td>
<td width="13%" align="left"><input type="time" name="hora_salida"></td>
<td width="12%" align="right">Destino:</td>
<td width="13%" align="left"><input type="text" name="destino" size="12"></td>
<td width="12%" align="right">Hora Llegada:</td>
<td width="13%" align="left"><input type="time" name="hora_llegada"></td>
</tr>
</table>
<input type="button" value="Eliminar"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />
</div>
<span id="writeroot"></span>
<input type="button" id="moreFields" value="Añadir ruta" />
<input type="submit" value="Grabar Ruta" />
<?php echo form_close(); ?>
Oh I can solve my problem I just need to use array names for the dynamic components in the view:
<?php echo form_open('admin/grabar_ruta'); ?>
<table width="100%">
<tr>
<td width="12%" align="right">Fuente:</td>
<td width="13%" align="left"><select name="fuente">
<?php foreach($fuentes as $row) { echo '<option value="'.$row->id.'">'.$row->serial.'</option>'; } ?> </select></td>
<td width="12%" align="right">Vehículo:</td>
<td width="13%" align="left"><select name="vehiculo">
<?php foreach($vehiculos as $row) { echo '<option value="'.$row->id.'">'.$row->placa.'</option>'; } ?> </select></td>
<td width="12%" align="right">Conductor:</td>
<td width="13%" align="left"><select name="conductor">
<?php foreach($conductores as $row) { echo '<option value="'.$row->id.'">'.$row->nombre.'</option>'; } ?> </select></td>
<td width="12%" align="right">Fecha:</td>
<td width="13%" align="left"><input type="date" name="fecha"></td>
</tr>
</table>
<div id="readroot" >
<table width="100%">
<tr>
<td width="12%" align="right">Origen:</td>
<td width="13%" align="left"><input type="text" name="origen[]" size="12"></td>
<td width="12%" align="right">Hora Salida:</td>
<td width="13%" align="left"><input type="time" name="hora_salida[]"></td>
<td width="12%" align="right">Destino:</td>
<td width="13%" align="left"><input type="text" name="destino[]" size="12"></td>
<td width="12%" align="right">Hora Llegada:</td>
<td width="13%" align="left"><input type="time" name="hora_llegada[]"></td>
</tr>
</table>
<input type="button" value="Eliminar"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />
</div>
<span id="writeroot"></span>
<input type="button" id="moreFields" value="Añadir ruta" />
<input type="submit" value="Grabar Ruta" />
<?php echo form_close(); ?>

Multiplying values using javascript

i have used js to multiply but only 1st row is getting multiplied other rows are nt getting multiplied.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("form1", $con);
error_reporting(E_ALL ^ E_NOTICE);
$nam=$_REQUEST['select1'];
$row=mysql_query("select * from inv where name='$nam'");
while($row1=mysql_fetch_array($row))
{
$Name=$row1['Name'];
$Address =$row1['Address'];
$City=$row1['City'];
$Pincode=$row1['Pincode'];
$No=$row1['No'];
$Date=$row1['Date'];
$DCNo=$row1['DCNo'];
$DcDate=$row1['DcDate'];
$YourOrderNo=$row1['YourOrderNo'];
$OrderDate=$row1['OrderDate'];
$VendorCode=$row1['VendorCode'];
$SNo=$row1['SNo'];
$descofgoods=$row1['descofgoods'];
$Qty=$row1['Qty'];
$Rate=$row1['Rate'];
$Amount=$row1['Amount'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
**function ram()
{
var q=document.getElementById('qty').value;
var r=document.getElementById('rate').value;
document.getElementById('amt').value=q*r;
}**
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="1315" border="0">
<script type="text/javascript">
function g()
{
form1.submit();
}
</script>
<tr>
<th>Name</th>
<th align="left"><select name="select1" onchange="g();">
<option value="" selected="selected">select</option>
<?php $row=mysql_query("select Name from inv ");
while($row1=mysql_fetch_array($row))
{ ?>
<option value="<?php echo $row1['Name'];?>"><?php echo $row1['Name'];?></option>
<?php } ?>
</select></th>
</tr>
<tr>
<th>Address</th>
<th align="left"><textarea name="Address"><?php echo $Address;?></textarea></th>
</tr>
<tr>
<th>City</th>
<th align="left"><input type="text" name="City" value='<?php echo $City;?>' /></th>
</tr>
<tr>
<th>Pincode</th>
<th align="left"><input type="text" name="Pincode" value='<?php echo $Pincode;?>'></th>
</tr>
<tr>
<th>No</th>
<th align="left"><input type="text" name="No2" value='<?php echo $No;?>' readonly="" /></th>
</tr>
<tr>
<th>Date</th>
<th align="left"><input type="text" name="Date" value='<?php echo $Date;?>' readonly="" /></th>
</tr>
<tr>
<th>DCNo</th>
<th align="left"><input type="text" name="DCNo" value='<?php echo $DCNo;?>' readonly="" /></th>
</tr>
<tr>
<th>DcDate:</th>
<th align="left"><input type="text" name="DcDate" value='<?php echo $DcDate;?>' /></th>
</tr>
<tr>
<th>YourOrderNo</th>
<th align="left"><input type="text" name="YourOrderNo" value='<?php echo $YourOrderNo;?>' readonly="" /></th>
</tr>
<tr>
<th>OrderDate</th>
<th align="left"><input type="text" name="OrderDate" value='<?php echo $OrderDate;?>' readonly="" /></th>
</tr>
<tr>
<th width="80">VendorCode</th>
<th width="1225" align="left"><input type="text" name="VendorCode" value='<?php echo $VendorCode;?>' readonly="" /></th>
</tr>
</table>
<table width="1313" border="0">
<tr>
<td width="44"> </td>
<td width="71">SNO</td>
<td width="527">DESCRIPTION</td>
<td width="214">QUANTITY</td>
<td width="214">RATE/UNIT</td>
<td width="217">AMOUNT</td>
</tr>
<?php $i=1;
$row=mysql_query("select * from inv where Name='$nam'");
while($row1=mysql_fetch_array($row))
{
$descofgoods=$row1['descofgoods'];
$Qty=$row1['Qty'];
$Rate=$row1['Rate'];
$Amount=$row1['Amount'];
?>
<tr>
<td><input type="checkbox" name="checkbox" value="checkbox" /></td>
<td><input type="text" name="No" value='<?php echo $No;?>' readonly=""/></td>
<td><input type="text" name="descofgoods" value='<?php echo $descofgoods;?>' /></td>
<td><input type="text" name="qty" maxlength="50000000" id="qty"/></td>
<td><input type="text" name="Rate" value='<?php echo $Rate;?>' id="rate" onclick="ram()";></td>
<td><input type="text" name="Amount" id="amt"/></td>
</tr>
<?php $i++;} ?>
<tr>
<th colspan="2">Print</th>
</tr>
</table>
<label></label>
</form>
</body>
</html>
I didn't read all of your code but i think problem is with your tag id it must be unique.
var q=document.getElementById('qty').value;
var r=document.getElementById('rate').value;
document.getElementById('amt').value=q*r;
javascript didn't get the other tag as you have more than one Tag with id 'qty', 'rate', 'amt', so javascript only convert the tag which comes first on the page and only multiple it.
TO AVOID THIS YOU SHOULD MAKE SURE THAT YOUR id FOR EACH TAG MUST BE UNIQUE
EDITED
1] Change your javascript function like something
function ram(id)
{
var q=document.getElementById('qty_'+id).value;
var r=document.getElementById('rate_'+id).value;
document.getElementById('amt_'+id).value=q*r;
}
2] add suffix $i to each tag
<td><input type="text" name="qty" maxlength="50000000" id="qty_<?PHP echo($i) ?>"/></td>
<td><input type="text" name="Rate" value='<?php echo $Rate;?>' id="rate_<?PHP echo($i) ?>" onclick="ram('<?PHP echo($i) ?>')";></td>
<td><input type="text" name="Amount" id="amt_<?PHP echo($i) ?>"/></td>
3] pass argument of the row in onclick function
onclick="ram('<?PHP echo($i) ?>')"
The problem you are having is that you use getElementById for this situation where you have multiple rows. An Id should be unique, so therefor it will only get the "first" match when using getElementById.
Try using parseInt():
document.getElementById('amt').value= parseInt(q) * parseInt(r);

Categories

Resources