Forum Moderators: open

Message Too Old, No Replies

configure a button

to show result of calculation.

         

nabilino

2:45 pm on Nov 19, 2007 (gmt 0)

10+ Year Member



Hello webmasters,
Thanks fotiman and dabrowski for the good help.
I have one question...
How can I configure a button to show the result of a calculation?
the user is suppose to hit the "calculate" button to get the grand total.

Here is what I have but it's giving me errors.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>Page title</title>
<script type="text/javascript" language="JavaScript">

function check(delivery_type)
{
document.getElementById("answer").value=delivery_type
}

function calculate ()
{
var idx = document.ticket.location.selectedIndex;
var location = document.ticket.location[idx].value
var quantity = document.ticket.quantity.value;
var delivery_type = document.ticket.delivery_type.value;
var subtotal = document.ticket.subtotal.value;
var subtotal = (location*quantity);

document.ticket.subtotal.value = subtotal;

document.ticket.grandtotal.value = subtotal + delivery_type;

}

</script>

</head>
<body>
<form id="ticket" name="myform">
<h3>Ticket Cost Calculator </h3><br />
<br />

<table border="0">
<tr>
<td>Location:</td>
<td>Quantity:</td>
</tr>

<tr>
<td><select name="location" onChange="calculate()">
<option value="10">End zone $10</option>
<option value="20">Not bad $20</option>
<option value="30">Good $30</option>
</select></td>
<td><input type="text" name="quantity" ></td>
</tr>

<tr>
<td>Subtotal:</td>
<td><input type="text" name="subtotal" readonly="readonly"></td>
</tr>

<tr>
<td><input type="radio" name="delivery_type" onchange="check(this.value)" value="15">Will call ($5) or</td>
<td><input type="text" name="answer" readonly="readonly"></td>
</tr>

<tr>
<td><input type="radio" name="delivery_type" onchange="check(this.value)" value="5">Overnight Delivery ($15)</td>
<td>&nbsp;</td>
</tr>

<tr>
<td>GrandTotal:</td>
<td><input type="text" name="grandtotal" readonly="readonly"></td>
</tr>

<tr>
<td><input type="button" name="calculate" value="Calculate" onClick="calculate(document.getElementById('ticket').grandtotal.value);"> </td>
<td> <input type="reset" value="Clear"></td>
</tr>
</table>

</form>

</body>
</html>

Fotiman

4:42 pm on Nov 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Why are you using XHTML? I would recommend you read the thread Why most of us should NOT use XHTML [webmasterworld.com].
2. Your <?xml> declaration will cause problems with IE.
3. Stop using the "language" attribute in your script tags. It's invalid and doesn't do anything.
4. You should validate [validator.w3.org] your HTML.
5. Remove your <br> (<br />) tags. They are presentational (use CSS for that).
6. Add an action to your form element.

Also, as I already mentioned in one of your other threads, there is a fully functional example in your thread with the subject "Question!" [webmasterworld.com]

nabilino

4:47 pm on Nov 19, 2007 (gmt 0)

10+ Year Member



i'm very new to javascript and programming overall. The reason I'm using xhtml is because it's a homework requirement. My prof thinks it's better than html for some reason. so here, i'm using html kit to write the code and i'm following his examples of codes.
I appreciate the working prototype in the other thread but I would like to get this one working so that i'm going by the rules.

Fotiman

5:02 pm on Nov 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your professor sounds like an idiot. Is that really what's being taught today? Table based layouts in XHTML? Ugh. I hate to tell you this, but I think you're pouring money into a very substandard education.

In any case, look at the computeGrandTotal method in my other thread. That does (I think) exactly what you are trying to do. All you need to do is tie that method to the event of your choice. In this case, it sounds like you want it to happen on a button 'onclick' event.

As I posted in the other thread, you're better to attach your event handlers via JavaScript instead of using inline event handlers. Also, if you're using XHTML, then all of your attributes must be lowercase (meaning if you're going to put them inline, then it needs to be 'onclick=...' instead of 'onClick=...'). If you validate your page, it will tell you where these errors are.