Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Code to Sum Rows PHP Update Form

    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
    Attached Thumbnails Attached Thumbnails Javascript Code to Sum Rows PHP Update Form-table1.jpg  

  • #2
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    <?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.
    Last edited by Icyhot; 10-05-2008 at 05:22 AM.

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <?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)); ?>


    Code:
    <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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •