Jump to content

Javascript to Sum Rows of a PHP Multiple Record Update Form


ningboring

Recommended Posts

I have created a multiple record update form using DWMX.  I have 8 fields (id, name, pro, prs, org, cnt, lng, and tot).  All fields, except name, are numeric (int or float) with 'tot' being the total of all the numeric fields (not including id).  Before submitting the update form I'd like Javascript to calculate the total so that it can be updated to the DB when the submit button is clicked.  I have successfully done this with an update form that deals with a single record, but how would I do this for more than one record at a time?  Any help greatly appreciated.

 

The Javascript for the single record form is below:

<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 = parseFloat(ipro) + parseFloat(iorg) + parseFloat(iprs) + parseFloat(icnt) + parseFloat(ilng);
document.getElementById('tot').value = itot;
}
</script>

Here is the relevant PHP code for the multiple update form:

if(isset($_POST["name"])) {for($i=0; $i < count($_POST["name"]); $i++) {
  $updateSQL = sprintf("UPDATE results SET name=%s, pro=%s, org=%s, prs=%s, cnt=%s, lng=%s, tot=%s WHERE id=%s",
                       GetSQLValueString($_POST["name"][$i], "text"),
                       GetSQLValueString($_POST["pro"][$i], "int"),
                       GetSQLValueString($_POST["org"][$i], "int"),
                       GetSQLValueString($_POST["prs"][$i], "int"),
                       GetSQLValueString($_POST["cnt"][$i], "int"),
                       GetSQLValueString($_POST["lng"][$i], "int"),
                       GetSQLValueString($_POST["tot"][$i], "int"),
                       GetSQLValueString($_POST["id"][$i], "int"));

  mysql_select_db($database_myconn, $myconn);
  $Result1 = mysql_query($updateSQL, $myconn) or die(mysql_error());

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

Ningboring

Link to comment
Share on other sites

I have a multiple record update form generated by 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.[/img]

 

Ningboring

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.