Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Can't get output to work

1. ## Can't get output to work

Hi All!!

Having problems getting data to out textarea quote. I've read heaps of tutorials on variables etc but i can't find any that show how calculate using heaps of variables only 2 or 3.

This is for an assignment and we have done minimal class work on javascript and my text books and online references for my class don't have any instruction on how to do what's required. I've searched the web and changed my code 100 times and the more changes I make it just seems to be getting worse.

What the code is meant to do:
Output quantity.
calculate price if option 1 or 2 is selected.
If card or giftwrapping is selected add that to the price for each item. E.G 20 items + 20 cards.
Is it a once only order or ongoing.
If ongoing is it monthly, six monthly or yearly - Apply discount to total of 5,4,3% respectively.
Is the service for 1,2 or 3 years - and apply a discount of 2% for each year off the order total.
Calculate the number of items that would be ordered for the entire period.
E.G. Quantity = 20. Option = 2. 20x2 = 40. 40 x 12 (monthly) x 2 years.
Total quantity = 960 for total period.
Output total Price after additions and discounts.

Code:
```<html>
<title>
Subscriptions
</title>

<style>
body {
background:#000000;
}
</style>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--Changed body tag to head-->
<head>

<script language="JavaScript" type="text/javascript">
function filladdress(f) {
if(f.addsame.checked == true) {
f.daddress.value = f.address.value;
f.dcity.value = f.city.value;
f.dpostcode.value = f.postcode.value;
}
}
</script>

<script language="JavaScript" type="text/javascript">

function calculateTotal()//calculation model to calculate output
{
var quantity
var numberVariable
var card
var Option = 1
var numberYears = "0"
var numberCost = "0"
var wordNo = "0"
var giftwrap
var gst = "0"
var gstFree = "0"
var totalCost = "0"
var servicePeryear = "0"
var numberMonthly
var numberHalfyearly
var numberYearly

}

if (quantity <= 30) {
numberVariable = 10*quantity;
}
else if (option){
numberVariable = 15*quantity + option;
}

else if (quantity> 30 && quantity<=60) {
numberVariable = 8*quantity;
}
else if (option){
numberVariable = 12*quantity + option2;
}
else if (quantity>60) {
numberVariable =8*quantity;
}
else if (option){
numberVariable = 11*quantity + option2;
}

if (card ==Yes){
numberCost = numberVariable + (quantity*2)
}

if (giftwrap == true)
{
giftwrap = numberCost * 1.03;
}

if (totalService >0)
{
numbersYears * 0.95;
servicesperyear = 12;
numberMonthly;
}
else if ()

document.subscriptions.quote.value = ('Business Name:\t\t' + A + '\nAddress:\t\t'

+ B + '\nTown/City:\t' + C + '\nPostcode:\t' + D + '\nDelivery Address:\t' + E +

'\nService Type:\t\t' + G + '\nService Interval:\t' + H + '\nContract Period:\t'

+ I + '\nquantity:\t' + J + '\nOption:\t\t' + K + '\nCard Included:\t' + L +

'\nGift Wrap:\t' + M + '\nTotal Services:\t\t' + servicenumber + '\nCost Per

Service:\t\$' + cost + '\nTotal Before GST:\t\$' + gstfree + '\nGST:\t\t\t\$' + gst

+ '\nGrand Total:\t\t\$' + totalcost)
}
</script>

<meta http-equiv="content-type" content="text/html; " />

<script language="JavaScript" /javascript">
\$(document).ready(
var winSize = \$(window).width();
var margin = winSize % 10;
function setMargin(){
\$('#main').css({'margin-left':margin,'margin-right':margin});
}
)
</script>
</head>
<form name=subscriptions>
<body>
<div id="main" align="middle" style="width:1000px; height:50%; text-align:left;

border: #000 solid 1px; margin:auto;">

<div id="inner1" style="width:100%; height:20%; background-color:#E6E6FA;

float:top;  border: #000 solid 1px;">

<div id="inner11" align="center"><br/>Quote</div>
</div>

<div id="inner2" style="width:100%; height:200%; background-color:#FFFAF0;

border: #000 solid 1px;">

<div id="inner21" align="left" style="width:70%; background-color:#F0F8FF;

float:left;"></div>

<form action="" id="buffetquote" onsubmit="return false;">

<fieldset><legend>Billing Information</legend>

Business Name:<input name="name"size="30"/>

Address:<input name=address size="30"/>

Town/City:<input name=city size="30"/>

Postcode:<input name=postcode size="4" maxlength="4"/><br/>

</fieldset>
<br>
<fieldset><legend>Contact Details</legend>
Contact Person:<input "
name="name"size="30"/>

Contact Number:<input "
name="phone number"size="30"/><br/>
</fieldset>
<br/>
<fieldset> <legend>Delivery Information</legend>
Tick if same as Billing Address
<input type=checkbox name=addsame onclick=filladdress(this.form)>

Delivery Address:<input name=daddress size="30"/>

Town/City:<input name=dcity size="30"/>

Postcode:<input name=dpostcode size="4" maxlength="4"/><br/>
</fieldset>
<br/>

<div id="onceonly" align="left">ONCE ONLY SERVICE:</div>

Quantity:<input name="quantity"size="3" maxlength="3"/><br/>
<br/>

Option: 1<input type="radio"  name="option" value=" 1" onclick="calculateTotal()"

/>
2<input type="radio"  name="option" value="2" onclick="calculateTotal()"

/><br/>
<br/>
Card <input type=checkbox name=card value=0 onclick="calculateTotal()"/>

Giftwrap <input type=checkbox name="giftwrap" value=0 />
<br/><br>
Service Type<br>
Once Only <input type=radio name=frequency value=o /> Regular <input type=radio

name=frequency value=r /><br> <br>

<div id=ongoing>
Number of Services:<br/>

Monthly<input type="radio"  name="schedule" value="12" onclick="calculateTotal()"

/>

6 Monthly<input type="radio" name="schedule" value="2" onclick="calculateTotal()"

/>

Yearly<input type="radio"  name="schedule" value="1" onclick="calculateTotal()"

/><br/>
<br/>

Number of Years:<br/>

1 Year<input type="radio"  name="years" value="1"onclick="calculateTotal()" />

2 Years<input type="radio" name="years" value="2" onclick="calculateTotal()" />

3 Years<input type="radio"  name="years" value="3" onclick="calculateTotal()" />

<br/>
</div>

<p><input type="button" name="calculate" value="Calculate"

onclick="calcaulateTotal" />

<input name="reset" type="reset" value="Reset" onclick="resetfrom" /></p>

</div>

<div id="inner3" style="width:100%; height:100%; float:bottom; background-

color:#FFFAF0; border: #000 solid 1px;">

<div id="inner23" align="left">Quotation</div>

<textarea name=quote rows=10 cols=100 > </textarea>

</script>

</body>
</html>```
Please don't laugh I know it's bad, lol. Any assistance would be much appreciated.

2. Have you tried using your error console? There are quite a few errors, including spelling errors onclick="calcaulateTotal". You <head> tag is in the wrong place - should come after <html>.

<meta http-equiv="content-type" content="text/html; " />
is in the wrong place.

The name of the form must be in quotes - <form name= "subscriptions">
Likewise <div id="ongoing">
Likewise name="frequency" value="r" // the value is the literal letter r

Your script does not capture the values entered by the user.
var quantity // just declares the variable.
You want var qty= document.subscriptions.quantity.value; // do not use the same name for an HTML element and a Javascript variable.

You have two field with the same name - name. Ugh! You should avoid giving names or id's to your variables/functions/arguments/forms words which are HTML/JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'click' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

As you say, it is bad. You say "we have done minimal class work on javascript" and I am afraid it shows. I do not understand why colleges expect to set assignments without providing the proper teaching and textbooks.

Tip - <script language="JavaScript"> is long deprecated.

"Knowledge is of no value unless you put it into practice.” - Anton Chekhov (Russian playwright and master of the modern short story, 1860-1904)

3. Hi PHilip, thank-you for getting back to me so quickly.

Will clean up the code, remove the duplicate the names and enter the correct values for the var's. I've actually recoded this so many times I didn't know which code to submit for you guys to look at and have most likely submitted the worst of them all.

Am I on the right track for the Quantity /cost calculations or are they way off too. Am I referencing option 1 and 2 correctly to calculate the price depending on which is required.

4. To put this right would take a good deal of time that I do not have and amount to doing your homework for you, which is discouraged in this forum. But I have to say that there are so many errors that a complete re-work is required.

I suggest you make a serious effort, re-work your code, try testing it yourself, use the error console to detect errors and then if necessary post it here again.

Javascript is case sensitive so Option is not the same as option.
else if (option){
means if option has (any) assigned value (not undefined).
if (card ==Yes){
means if the variable card has the same value as another varaible named Yes
var numberYears = "0"
assigns the string value "0" to numberYears, not the numeric value 0
You have two forms (why?) one named subscriptions (not in quotes) and one with id="buffetquote"
Neither have a </form> tag.

... and more. I am sorry to say that is ths script was a horse it would have to be shot to put it out of its misery.

5. Lol, was just about to post that I'll redo it all over again.

Think it will be easier.

I also agree with the homework, I've told heaps of kids in the past that we can't do their homework for them when they post on forums. I'll assist them to find what they're looking for but I wont do it for them.

I do ICT not web development. If a server's fried, I can fix it and get it back online yesterday, makes no sense that js is a requirement for this course. If it was a web development course I could understand.

Thank-you for your help.

#### Posting Permissions

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