0

can anyone tell me what's wrong is it. thanks

Firebug showed error said that "TypeError: price is undefined." howmany = parseInt(price.value); }

the purpose of code:

I use javascript to calculate the total price for my contact-form. where the price and quantity is come from the form.

HTML Part:

<form id="contact-form" action="/email_form.php" method="post">
<table>
<tr ><td>   CAKE A  </td>
<td><input name="p_A01" id="p_A01" value="420" readonly="readonly"  type="tel" /></td>  
<td><input name="q_A01" id="q_A01"  type="tel" onchange="calculateTotal(); /> </td></tr>

<tr ><td>   CAKE B  </td>
<td><input name="p_A02" id="p_A02" value="420" readonly="readonly"  type="tel" /></td>  
<td><input name="q_A02" id="q_A02"  type="tel" onchange="calculateTotal(); /> </td></tr>

.
.
.
  etc



</table>
<input name="submit" type="submit"  />
</form>

Javascript Part: GetQuantity() is used to get the value from the Qty field. e.g. q_A01..

GetPrice() is used to get the readonly value Price field. e.g. p_A01..

calculateTotal() is used to calculate the total price and return to the field ID "Total".

function GetQuantity(e) {
var theForm = document.forms["contact-form"];
var quantity = theForm.elements[e];
var howmany =0;
if(quantity.value!=0) {
howmany = parseInt(quantity.value); }
return howmany;
}
function GetPrice(e) {
    var theForm = document.forms["contact-form"];
    var price = theForm.elements[e];
    var howmany =0;
    if(price.value!=0) {
    howmany = parseInt(price.value); }
    return howmany;
    }

function calculateTotal()
{
    var cakePrice =
GetPrice(p_A01)*GetQuantity(q_A01)+
GetPrice(p_A02)*GetQuantity(q_A02)+
GetPrice(p_A03)*GetQuantity(q_A03)+
GetPrice(p_F11)*GetQuantity(q_F11);

  var Totalordered = document.getElementById ("Total");
         Totalordered.value = cakePrice;


}
Chiwai Kwan
  • 5
  • 1
  • 3
  • Possible duplicate of [Detecting an undefined object property](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Hanky Panky Apr 04 '16 at 08:04
  • The exception explains it, your `price` variable is undefined. Check for it existence after trying to get it and handle the error appropriately. – ste2425 Apr 04 '16 at 08:06

1 Answers1

0
GetPrice(p_A01)*GetQuantity(q_A01)+
GetPrice(p_A02)*GetQuantity(q_A02)+
GetPrice(p_A03)*GetQuantity(q_A03)+
GetPrice(p_F11)*GetQuantity(q_F11);

should be

GetPrice("p_A01")*GetQuantity("q_A01")+
GetPrice("p_A02")*GetQuantity("q_A02")+
GetPrice("p_A03")*GetQuantity("q_A03")+
GetPrice("p_F11")*GetQuantity("q_F11");

because the IDs are strings.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102