Multiplying values using javascript - 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);

Related

how to fetch the data from database in dynamically created table using codeiginter

<table class="table table-bordered table-striped table-xxs" id="tb3" name="tb3">
<thead>
<tr>
<th></th>
<th>Product Code</th>
<th>Product Name</th>
<th>Qty</th>
<th>Rate</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr >
<td><a href='javascript:void(0);' class='remove3'><span class='glyphicon glyphicon-remove'></span></a></td>
<td>
<input style="width:80px" type="text" id="Product_Code" class="form-control input-xs Product_Code " onkeyup="fetch()" value="<?php echo $r->Product_Code; ?>" name="Product_Code[]" required></td>
<td ><input style="width:300px" type="text" id="Product_Name" class="form-control input-xs" value="<?php echo $r->Prdtname; ?>" name = "Prdtname[]" value=""> </td>
<td><input style="width:80px" type="text" id="Qty" class="form-control input-xs" value="<?php echo $r->Qty; ?>" name="Qty[]" required></td>
<td><input style="width:100px" type="text" id="Rate" class="form-control input-xs" value="<?php echo $r->rate; ?>" name="rate[]" required></td>
<td><input style="width:150px" type="text" id="Value" class="form-control input-xs" value="<?php echo $r->amount; ?>" name="amount[]" ></td>
<th><span class="glyphicon glyphicon-plus"></span></th>
</tr>
</tbody>
</table>
this table code....
<script>
$(document).ready(function (){
$('#addMore3').on('click', function() {
var data = $("#tb3 tr:eq(1)").clone(true).appendTo("#tb3");
data.find("input").val('');
});
$(document).on('click', '.remove3', function() {
var trIndex = $(this).closest("tr").index();
if(trIndex>0) {
$(this).closest("tr").remove();
} else {
alert("Sorry!! Can't remove first row!");
}
});
});
</script>
this is javascript code for creating table by clicking "+" event.
My prblm is how to fetch data from database display in this automatic table..
You should be able to do this using ajax call.
As you are using codeigniter -
View : Write your ajax and table generation code.
Controller: get the ajax request and pass it to modal.
Modal: get the data from database.
you should return array of object to view and then just parse the data and generate table.
If you dont want to write the table code and ajax then you can use datatable plugin.

How to get the value in Textbox from database depends on dropdown list without submit button

I want to get the text box value from database depends upon the dropdownlist values without using submit button .Now I have three dropdown list on my code.how can i write the javascript for three dropdown
<div align="center">
<table>
<thead>
<th>Product</th>
<th>Quantity</th>
<th>Rate</th>
<th>Amount</th>
</thead>
<tbody>
<tr id="addrow">
<td>
<?php
$selectproduct=mysql_query("select productname from items");
$itemname=mysql_fetch_array($selectproduct);
?>
<select name="item[]" id="item">
<?php
$i=0;
while($itemname=mysql_fetch_array($selectproduct))
{
?>
<option value="<?php echo $itemname['productname'];?>"><?php echo $itemname['productname'];?></option>
<?php
$i++;
}
?>
</td>
<?php
// $amount=mysql_query("select Amount from items where Productname='$productname' ");
// $totalamount=mysql_fetch_array($amount);
?>
<td><input class="qty" type="text" name="qty[]"></td>
<td><input class="price" type="text" name="price[]"></td>
<td><input type="text" class="output" name="output[]"></td>
</tr>
<tr >
<td>
<?php
$selectproduct=mysql_query("select productname from items");
$itemname=mysql_fetch_array($selectproduct);
?>
<select name="item[]" id="item">
<?php
$i=0;
while($itemname=mysql_fetch_array($selectproduct))
{
?>
<option value="<?php echo $itemname['productname'];?>"><?php echo $itemname['productname'];?></option>
<?php
$i++;
}
?>
</td>
<td><input class="qty" type="text" name="qty[]"></td>
<td><input class="price" type="text" name="price[]"></td>
<td><input type="text" class="output" name="output[]"></td>
</tr>
<tr>
<td>
<?php
$selectproduct=mysql_query("select productname from items");
$itemname=mysql_fetch_array($selectproduct);
?>
<select name="item[]" id="item">
<?php
$i=0;
while($itemname=mysql_fetch_array($selectproduct))
{
?>
<option value="<?php echo $itemname['productname'];?>"><?php echo $itemname['productname'];?></option>
<?php
$i++;
}
?>
</td>
<td><input class="qty" type="text" name="qty[]"></td>
<td><input class="price" type="text" name="price[]"></td>
<td><input type="text" class="output" name="output[]"></td>
</tr>
</table>
<div id="grand">
Total Amount:<input type="text" name="gran" id="gran">
</div>
</tr>
<tr >
<td colspan="4">
<center><input type="submit" name="submit"></center>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<script type="text/javascript">
$(document).ready(function() {
$(".price").keyup(function() {
var grandTotal = 0;
$("input[name='qty[]']").each(function (index) {
var qty = $("input[name='qty[]']").eq(index).val();
var price = $("input[name='price[]']").eq(index).val();
var output = parseInt(qty) * parseInt(price);
if (!isNaN(output)) {
$("input[name='output[]']").eq(index).val(output);
grandTotal = parseInt(grandTotal) + parseInt(output);
$('#gran').val(grandTotal);
}
});
});
});
</script>
I recommand you to use jQuery for start. It is easier to manipulate the DOM.
Follow a tutorial and then, attach a callback on the change event on your dropdown menus, then in the callback get the values for your menu in an ajax call, then generate the html (the option tags) and update your select.
The Ajax call and the html update can be done with jQuery, but you can do it by yourself in pure Javascript as well.
You'll need to use AJAX calls from JS. AJAX has to send and receive data from php script. Simple AJAX tutorial: https://www.w3schools.com/js/js_ajax_php.asp

jQuery get button classname on button click

I am trying to assign textbox a value on button click. I am assigning the value to previous textbox of button.
I used
var classic = window.Event.target.class;
alert(classic);
but this gives error undefined.
How can I identify which button clicked either by class name or id or some other way.
jQuery(document).ready(function() {
var formfield;
var classic = window.Event.target.class;
/* user clicks button on custom field, runs below code that opens new window */
jQuery(classic).click(function() {
formfield = jQuery(this).prev('input'); //The input field that will hold the uploaded file url
tb_show('','media-upload.php?TB_iframe=true');
return false;
});
//adding my custom function with Thick box close function tb_close() .
window.old_tb_remove = window.tb_remove;
window.tb_remove = function() {
window.old_tb_remove(); // calls the tb_remove() of the Thickbox plugin
formfield=null;
};
// user inserts file into post. only run custom if user started process using the above process
// window.send_to_editor(html) is how wp would normally handle the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html){
if (formfield) {
fileurl = jQuery('img',html).attr('src');
jQuery(formfield).val(fileurl);
tb_remove();
} else {
window.original_send_to_editor(html);
}
};
});
<form method="post" action="options.php">
<?php settings_fields( 'Option-Man-settings-group' ); ?>
<?php do_settings_sections( 'Option-Man-settings-group' ); ?>
<div class="frame">
<table class="form-table">
<tr valign="top">
<th class="label">Logo URL:</th>
<td><input type="text" name="logo-setting" size="45" value="<?php echo esc_attr( get_option('logo-setting') ); ?>" /></td>
<td><input name="op" class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Header:</th>
<td><input type="text" name="main-header" size="45" value="<?php echo esc_attr( get_option('main-header') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Carousel-url:</th>
<td><input type="text" name="carousel-image" size="45" value="<?php echo esc_attr( get_option('carousel-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Message:</th>
<td><input type="text" name="message-short" size="45" value="<?php echo esc_attr( get_option('message-short') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Question header:</th>
<td><input type="text" name="question-cur" size="45" value="<?php echo esc_attr( get_option('question-cur') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Happy Clients Post Image:</th>
<td><input type="text" name="happy-image" size="45" value="<?php echo esc_attr( get_option('happy-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Happy Clients Post Header:</th>
<td><input type="text" name="happy-header" size="45" value="<?php echo esc_attr( get_option('happy-header') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Happy Clients Post Text:</th>
<td><input type="text" name="happy-text" size="45" value="<?php echo esc_attr( get_option('happy-text') ); ?>" /></td>
</tr>
</tr>
<tr valign="top">
<th scope="row" class="label">Solution Builder Post Image:</th>
<td><input type="text" name="builder-image" size="45" value="<?php echo esc_attr( get_option('builder-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Solution Builder Post Header:</th>
<td><input type="text" name="builder-header" size="45" value="<?php echo esc_attr( get_option('builder-header') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Solution Builder Post Text:</th>
<td><input type="text" name="builder-text" size="45" value="<?php echo esc_attr( get_option('builder-text') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Agile Process Post Image:</th>
<td><input type="text" name="agile-image" size="45" value="<?php echo esc_attr( get_option('agile-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Agile Process Post Header:</th>
<td><input type="text" name="agile-header" size="45" value="<?php echo esc_attr( get_option('agile-header') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Agile Process Post Text:</th>
<td><input type="text" name="agile-text" size="45" value="<?php echo esc_attr( get_option('agile-text') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Honesty Transparency Post Image:</th>
<td><input type="text" name="honesty-image" size="45" value="<?php echo esc_attr( get_option('honesty-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Honesty Transparency Post Header:</th>
<td><input type="text" name="honesty-header" size="45" value="<?php echo esc_attr( get_option('honesty-header') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Honesty Transparency Post Text:</th>
<td><input type="text" name="honesty-text" size="45" value="<?php echo esc_attr( get_option('honesty-text') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">Medtegra Image:</th>
<td><input type="text" name="Client1-image" size="45" value="<?php echo esc_attr( get_option('Client1-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">HDFC Image:</th>
<td><input type="text" name="Client2-image" size="45" value="<?php echo esc_attr( get_option('Client2-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">iitjobs Image:</th>
<td><input type="text" name="Client3-image" size="45" value="<?php echo esc_attr( get_option('Client3-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">ProductiveTeams.com:</th>
<td><input type="text" name="Client4-image" size="45" value="<?php echo esc_attr( get_option('Client4-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
<tr valign="top">
<th scope="row" class="label">JFYS Image:</th>
<td><input type="text" name="Client5-image" size="45" value="<?php echo esc_attr( get_option('Client5-image') ); ?>" /></td>
<td><input class="option-man button" type="button" value="Upload Image" /></td>
</tr>
</tr>
<tr valign="top">
<th scope="row" class="label">Clients Message:</th>
<td><input type="text" name="Client-message" size="45" value="<?php echo esc_attr( get_option('Client-message') ); ?>" /></td>
</tr>
</tr>
</tr>
<tr valign="top">
<th scope="row" class="label">Google+:</th>
<td><input type="text" name="google" size="45" value="<?php echo esc_attr( get_option('google') ); ?>" /></td>
</tr>
</tr>
</tr>
<tr valign="top">
<th scope="row" class="label">Twitter:</th>
<td><input type="text" name="twitter" size="45" value="<?php echo esc_attr( get_option('twitter') ); ?>" /></td>
</tr>
</tr>
</tr>
<tr valign="top">
<th scope="row" class="label">Facebook:</th>
<td><input type="text" name="facebook" size="45" value="<?php echo esc_attr( get_option('facebook') ); ?>" /></td>
</tr>
</tr>
</tr>
<tr valign="top">
<th scope="row" class="label">Linkedin:</th>
<td><input type="text" name="linkedin" size="45" value="<?php echo esc_attr( get_option('linkedin') ); ?>" /></td>
</tr>
</table>
</div>
<?php submit_button(); ?>
</form>
I believe this is what you want.
$('.form-table').on('click', 'button, input[type="button"]', function () {
// Get the previous input
var input = $(this).closest('td').prev('td').find('input');
// Get this button class
alert($(this).attr('class'));
// Get this button id
alert($(this).attr('id'));
// Your code here
tb_show('', 'media-upload.php?TB_iframe=true');
return false;
});
Demo: https://jsfiddle.net/tusharj/o59eoomx/2/

Submit button not working because of formating

I have been working to get this code working for a little while and finally got it to work by implementing two codes together. Except the formatting of it does not let my submit button work. My submit button acts a save by updating all data entered into the textboxes in a sql database. So this button is absolutely necessary. Please let me know what I'm doing wrong.
<?php
mysql_connect("localhost", "username", "password")or die("cannot connect");
mysql_select_db("databas")or die("cannot bselect DB");
$sql="SELECT * FROM test";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<html>
<head>
<link rel="shortcut icon" href="../../favicon.ico">
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/component.css" />
<script src="javafile.js"></script>
</head>
<body>
<div class="component">
<table class="overflow-y">
<thead>
<tr>
<th width="16%" align="center" id="box_header2" style='width:10%'><button class="logout">Log out</button> Name</th>
<th width="16%" align="center" id="box_header2" style='width:10%'>Job Code</th>
<th width="16%" align="center" id="box_header2" style='width:10%'>Date</th>
<th width="14%" align="center" id="box_header2" style='width:20%'>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<form action="update3.php" method="post" name="form1">
<tbody>
<tr>
<th>
<input name="name[]" type="text" id="Name" value="<? echo $rows['Name'];?>">
</th>
<td>
<input name="job[]" type="text" id="Job" value="<? echo $rows['Job']; ?>">
</td>
<td>
<input name="date[]" type="text" id="Date" value="<? echo $rows['Date']; ?>">
</td>
<td>
<input name="address[]" type="text" id="Address" value="<? echo $rows['Address']; ?>">
</td>
<td>
<input name="id[]" type="hidden" value="<? echo $rows['ID']; ?>">
</td>
</tr>
</tbody>
<?php
}
?>
<tr>
</tr>
</tbody>
<tbody>
<tr>
<td>
<input type="submit" name="Submit" value="Save">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</form>
</table>
<?php
mysql_close();
?>
try this hope it works for you :
<input name="name[]" type="text" id="Name" value='<?php echo $rows["uname"];?>'>
Please, put the <form> tag before the while loop, otherwise you'll have as many opened forms as rows in the query's result set.

SUM dynamic input field onchange with 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;

Categories

Resources