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: please help me with this javascript form

1. ## please help me with this javascript form

hello everyone,
im new to javascripts and kinda strugggling with it

can anybody please tell me wht is wrong with the following code,
i have to creata an airline ticket price calculator form, i cant get it to work, i tried so many times.

anybody's help will be really appreciated, i hope you guys wouldnt mind me posting the code in here.
please please help

<html>
<head>
<title> Metropolis </title>

<script language="javascript">
<!-- // Calculate the cost of the flight
function Calculatecost()

{
var routeIndex;//index of the selected route
var fare;//cost of flights
var seatingMultiplier;//seating class multiplier

//determine the one-way fare for the selected route

routeIndex=document.flightForm.route.selectedIndex;
if (routeIndex==0)
{
alert("Select a route");
return;
}

else

{
fare=document.flightForm.route.options[routeIndex].value;
}

// Adjust the fare if a return flight
has been selected
if(document.flightForm.returnFare.checkbox)
{
fare=fare*2;
}

//adjust the fare for the selected seating class

seatingMultiplier=0;
for(var i=0; i<=2; i++)
{
if(document.flightForm.seating[i].checked);
seatingMultiplier=document.flightForm.seating[i].value;
}

if(seatingMultiplier==0)
{
alert("Please select a seating class");
return;
}
else
{
fare=fare*seatingMultiplier;
alert("The cost of the flight is \$ +fare);
}
//-->
</script>

</head>
<body>
<h1> Metropolis Airline Fare Calculator
</h1>

<p> Complete the form below to Calculate the cost of your flight:</p>

<form name="flightForm">
<p>Route:

<select name="route" size="1">
<option> </option>
<option value="210">Metropolis-Amity</option>
<option value="220">Metropolis-Bedrock</option>
<option value="180">Metropolis-Gotham</option>
<option value="150">Metropolis-Smallville</option>
<option value="250">Metropolis-Southpark</option>
<option value="200">Metropolis-Springfield</option>

</select>
</p>
<p>Click here if you wil be purchasing a return fare:
<input type="checkbox" name="returnFare">
</p>

<p> Seating:<br>
<input type="radio" name="seating" value="2">First Class</br>
<input type="radio" name="seating" value="1.5">Business Class</br>
<input type="radio" name="seating" value="1">Economy Class</br>
</p>

<p><input type="button" value="Calculate" onClick="Calculatecost()">
<input type="reset" value="Reset"></p>

</form>
</body>
</html>

• A quick look shows that you are
not assigning a value to these variables.

function Calculatecost()

{
var routeIndex;//index of the selected route
var fare;//cost of flights
var seatingMultiplier;//seating class multiplier

• Cleaned up a few typos - watch your closing brackets an' stuff. The following works for me:

Code:
```<html>
<head>
<title> Metropolis </title>

<script language="javascript">
<!-- // Calculate the cost of the flight
function Calculatecost(){
var routeIndex=document.flightForm.route.selectedIndex;
var fare;//cost of flights
var seatingMultiplier;//seating class multiplier

//determine the one-way fare for the selected route
if (routeIndex==0){
alert("Select a route");
return;
}
else{
fare=document.flightForm.route.options[routeIndex].value;
}

// Adjust the fare if a return flight has been selected
if(document.flightForm.returnFare.checked){
fare=fare*2;
}

//adjust the fare for the selected seating class

seatingMultiplier=0;
for(i=0; i<=2; i++){
if(document.flightForm.seating[i].checked){
seatingMultiplier=document.flightForm.seating[i].value;
}
}

if(seatingMultiplier==0){
alert("Please select a seating class");
return;
}
else
{
fare=fare*seatingMultiplier;
alert("The cost of the flight is \$"+fare);
}
}
//-->
</script>

</head>
<body>
<h1> Metropolis Airline Fare Calculator
</h1>

<p> Complete the form below to Calculate the cost of your flight:</p>

<form name="flightForm">
<p>Route:

<select name="route" size="1">
<option> </option>
<option value="210">Metropolis-Amity</option>
<option value="220">Metropolis-Bedrock</option>
<option value="180">Metropolis-Gotham</option>
<option value="150">Metropolis-Smallville</option>
<option value="250">Metropolis-Southpark</option>
<option value="200">Metropolis-Springfield</option>

</select>
</p>
<p>Click here if you wil be purchasing a return fare:
<input type="checkbox" name="returnFare">
</p>

<p> Seating:<br>
<input type="radio" name="seating" value="2">First Class</br>
<input type="radio" name="seating" value="1.5">Business Class</br>
<input type="radio" name="seating" value="1">Economy Class</br>
</p>

<p><input type="button" value="Calculate" onClick="Calculatecost()">
<input type="reset" value="Reset"></p>

</form>
</body>
</html>```

• ## thanks mate

thanks so much spudhead and willyduit,
appreciate it, yeha i admit i always make mistakes with the brackets, i feel dumb now(yeah im not very smart anyways)

•

#### Posting Permissions

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