...

View Full Version : Javascript Code to Sum Rows PHP Update Form



ningboring
10-05-2008, 03:38 AM
I have a multiple record update form generated using PHP. I would like to total the fields (rows) before submitting the values to the MYSQL database. I can get this working for individual records, but have no idea how to do it for multiple records. Any help greatly appreciated.

Ningboring

Icyhot
10-05-2008, 04:17 AM
I'm not sure enough of the particulars, but I assume you have some loop going from the PHP such as the following. What I'd do is while it's pulling the info from the database and spitting out html I'd include a hidden field or just a counter variable


<?php
$counter = 1;
$Recordset1 = mysql_query($query_Recordset1) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
do { ?>
<option value="<?php echo $row_Recordset1['Name']; ?>"><?php echo $row_Recordset1['Name']; ?></option>
<?php $counter++;} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</select>

Of course after you are done with the page and want to post it back you'll have to decide if you want to register the variable in a session or use the hidden field to pass the value.

ningboring
10-05-2008, 04:50 AM
Thanks for the prompt reply. Probably look at usiing a hidden field to pass the value. Hope this helps. The rather simple Javascript function used in the single update form is also attached. Here's the code I used to generate the update form:

<?php $counter=1;
do { ?>
<tr>
<td><input name="name<?php echo($counter);?>" type="text" id="name<?php echo($counter);?>" value="<?php echo $row_rs['name']; ?>" size="12"></td>
<td><input name="pro<?php echo($counter);?>" type="text" id="pro<?php echo($counter);?>" value="<?php echo $row_rs['pro']; ?>" size="12"></td>
<td><input name="org<?php echo($counter);?>" type="text" id="org<?php echo($counter);?>" value="<?php echo $row_rs['org']; ?>" size="12"></td>
<td><input name="prs<?php echo($counter);?>" type="text" id="prs<?php echo($counter);?>" value="<?php echo $row_rs['prs']; ?>" size="12"></td>
<td><input name="cnt<?php echo($counter);?>" type="text" id="cnt<?php echo($counter);?>" value="<?php echo $row_rs['cnt']; ?>" size="12"></td>
<td><input name="lng<?php echo($counter);?>" type="text" id="lng<?php echo($counter);?>" value="<?php echo $row_rs['lng']; ?>" size="12"></td>
<td><input name="tot<?php echo($counter);?>" type="text" id="tot<?php echo($counter);?>" value="<?php echo $row_rs['tot']; ?>" readonly= "true" size="12">
<input type="hidden" name="id<?php echo($counter);?>" value="<?php echo $row_rs['id']; ?>">
<?php $counter++;?>
</td>
</tr>
<?php } while ($row_rs = mysql_fetch_assoc($rs)); ?>


<script type="text/javascript">
function getGradeTotal() {
var ipro = document.form1.pro.value;
var iorg = document.form1.org.value;
var iprs = document.form1.prs.value;
var icnt = document.form1.cnt.value;
var ilng = document.form1.lng.value;
var itot = parseInt(ipro)+parseInt(iorg)+parseInt(iprs)+parseInt(icnt)+parseInt(ilng);
document.getElementById('tot').value = itot;
}
</script>
Thanks. Ningboring



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum