PDA

View Full Version : Hidden Element


viviblue
04-06-2004, 07:36 PM
hi there, how can i pass a variable for a totalcost which is displayed in a table and display it in a hidden form element.

sad69
04-06-2004, 08:17 PM
Need more info...

So you have a table, one of the cells holds the totalCost, and you'd like to grab the value from that cell and put it into a hidden input? That's sort of what I got out of it..

How is this table constructed? Where did it come from? If you could post some relevant pieces of code it may help...

Sadiq.

Roy Sinclair
04-06-2004, 09:20 PM
Based on the name of the field you want to hide, I'd like to add that if you're planning on passing that field to the user's browser and then using it as the basis for anything other than a simple comparison for auditing purposes then you're setting yourself up for a problem where a user deliberately alters the field and makes $.02 purchases out of a $10,000 invoice.

viviblue
04-07-2004, 07:27 PM
well basically the user has purchased say 2 items and the cost of these items is added up and stored in a variable totalcost. Im wanting to then put the varible value into a hidden form field so that the total cost is submitted with the users form details.

sad69
04-07-2004, 07:59 PM
So you've got the total cost stored in "a variable". How did you get it into this variable?

I'm trying to use common sense, but you've not given a lot to go on. This is my educated guess:

So the user comes to "this page" and they've already got their items that they want (shopping cart idea perhaps?). The user may now type in the quantity, you use javascript to come up with some total. Now your user is ready to go to the next page (or something requiring them to press the submit button). Am I right? This is the kind of story we need, with a little more detail yet..

My proposed solution (like most of my proposed solutions since I've discovered DOM!) is to use DOM:

//assumes total cost is stored in:
//var totalCost
...
<script>
function insertTotalCost() {
document.getElementById("totalCost_id").value = totalCost;
}
</script>
...
<input type="hidden" name="total_cost" id="totalCost_id"/>
...
<input type="submit" onClick="insertTotalCost()"/>
...


That should work.. if not instead of having the onClick event on the submit button, call insertTotalCost() on the onSubmit event on the form.

Hope that helps and makes sense (and that I guessed your problem correctly..),
Sadiq.

viviblue
04-07-2004, 08:15 PM
heres the code which calculates the total cost:

cost = opener.items[i].price * opener.items[i].quantity;
cost = Math.round(cost * 100) /100;
totcost += cost;
document.write("<TD><P>" + cost);
document.write("</TR>\n");
document.write("<INPUT TYPE=HIDDEN NAME='qty" + i + "'");
document.write(" VALUE='" + opener.items[i].quantity + "'>\n");
document.write("<INPUT TYPE=HIDDEN NAME='desc" + i + "'");
document.write(" VALUE='" + opener.items[i].desc + "'>\n");
}
document.write("<TR><TD COLSPAN=3><P>Total Cost:</P>");
document.write("<TD><P>" + totcost + "</TR>");
</SCRIPT>
</TABLE>
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="Submit Order">

Willy Duitt
04-07-2004, 08:21 PM
What's the problem?

You are already document.writing two other hidden form elements. Just document.write the totcost hidden form element and update that value either as cost is figured or onsubmit of the form.

......Willy

sad69
04-07-2004, 08:22 PM
Ok, so then do this:

document.write("<TD><P>" + totcost + "<INPUT TYPE='hidden' NAME='total_cost' VALUE='+ totcost +'/></TR>");


Sadiq.

viviblue
04-07-2004, 08:40 PM
<INPUT TYPE="hidden" NAME="total" Value="+ totcost +">

I have tried this but it wont work

viviblue
04-07-2004, 08:41 PM
got it boys thanks alot

viviblue
04-07-2004, 08:52 PM
ok so no it still wont work, what i tried just made multiple totals instead of just sending one value.

sad69
04-07-2004, 11:17 PM
Ok so post your code so that we may help you see where you went wrong...

Sadiq.