Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems in storing values to the database

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
0
0
PH
im currently working on a page wherein it make use of a table that computes for the sum of values for each row as well as getting the sum for an entire column and here is the code:

Code:
<script type="text/javascript">

function disen(c) {
    var otd=c.parentNode;
    var otr=otd.parentNode;

    for (var j=1;j<otr.cells.length;j++) {
        if (!c.checked) {
            otr.cells[j].getElementsByTagName("input")[0].value="";
        }
        otr.cells[j].getElementsByTagName("input")[0].disabled=!c.checked;
    }
}

function doit() {
    var otbl=document.getElementById("tblid");

    var sum, cv, nrow, ncell;
    for (var i=1;i<otbl.rows.length-1;i++) {
        sum=0;
        ncell=otbl.rows[i].cells.length-1;
        if (otbl.rows[i].cells[0].getElementsByTagName("input")[0].checked) {
            for (var j=1;j<ncell;j++) {
                cv=otbl.rows[i].cells[j].getElementsByTagName("input")[0].value;
                if (cv.length==0||!/^\s*[+-]?\d*(\.\d*)?\s*$/.test(cv)) {
                    otbl.rows[i].cells[j].getElementsByTagName("input")[0].value="";
                    cv=0;
                }
                sum+=parseFloat(cv);
            }
            otbl.rows[i].cells[ncell].getElementsByTagName("input")[0].disabled=false;
            otbl.rows[i].cells[ncell].getElementsByTagName("input")[0].value=sum;
        }
    }

    nrow=otbl.rows.length-1;    //last row
    ncell=otbl.rows[nrow].cells.length-1;    //last row's last cell

    for (var j=1;j<otbl.rows[nrow].cells.length;j++) {
        sum=0
        for (var i=1;i<otbl.rows.length-1;i++) {
			cv=otbl.rows[i].cells[j].getElementsByTagName("input")[0].value;
            if (cv.length==0||!/^\s*[+-]?\d*(\.\d*)?\s*$/.test(cv)) {
                otbl.rows[i].cells[j].getElementsByTagName("input")[0].value="";
                cv=0;
            }
            sum+=parseFloat(cv);
        }
        otbl.rows[nrow].cells[j].getElementsByTagName("input")[0].value=sum;
    }
}
</script>

</head>
<body>
<table border=0 cellpadding=1 cellspacing=5 id="tblid">    <!-- I add an id to facilitate the referencing. -->
<tr>
<th>Days of Month</th>
<th>Kids</th>
<th>Adults</th>
<th>Volunteers</th>
<th>subtotal</th>    <!-- added column -->
</tr>

<?php
for($i=1;$i<=31;$i=$i+1)
{
    echo '<tr>
        <td>'.$i.'&nbsp;&nbsp;&nbsp;<input type="checkbox" onclick="disen(this)"></td>
        <td><input type="text" disabled="disabled" size=5 ></td>
        <td><input type="text" disabled="disabled" size=5 ></td>
         <td><input type="text" disabled="disabled" size=5 ></td>
         <td><input type="text" disabled="disabled" size="5" readonly="readonly" /></td>
    </tr>';
}
?>
    <tr>
    <td><span style="font-weight:bold;">&nbsp;&nbsp;&nbsp;&nbsp;subtotal:</span></td>
    <td><input type="text" size="5" readonly="true" /></td>
    <td><input type="text" size="5" readonly="true" /></td>
    <td><input type="text" size="5" readonly="true" /></td>
    <td><input type="text" size="5" readonly="true" /></td>
    </tr>
</table>
<button onclick="doit()">update subtotals</button><br />

its working just fine now the problem is how will i be able to store the sum of those values in the database what could be best way to do it?
i know that most of the code deals with javascript but how will i integrate it with php

 
You can take the final value stick it in a form field and submit the form, then perform the insert to DB operations in the PHP side.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i need to get the value of sum generated by javascript to be able to store it in the database what will be my code for that... any idea how will i go about these?
thanks in advance....
 
Once your Javascript is done computing the sum. take that value and place it into a form field such as:

Code:
<form action="dbinsert.php" method=POST>
<input type=hidden name="sumresult">
</form>

then in your JS code something like:

document.forms[1].sumresult.value="final sum value here";
document.forms[1].submit();

With that you'll get your sum values sent over so your PHP can use them and insert them into a DB.

You can add as many hidden fields as you need. And then have JS automatically submit the form.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top