...

View Full Version : Using info from 3 different types of user input



Nicole
10-06-2003, 08:28 AM
.

glenngv
10-06-2003, 09:10 AM
post the codes here or attach it if it's too long

Nicole
10-07-2003, 01:40 AM
Here it is

Kor
10-07-2003, 10:44 AM
Ok, here's your code. Before that, beware... there were a lot of mistakes within your code, take care for the future... No offence, please :)

Take care, your brackets were incorrect. If use local variables var, maybe is better to put them inside the function. You forgot to give a name to a checkbox. If you nedd a veryfing condition, use return condition before calling a function, and the responsereturn true or return false for stop or continue the code. Otherwise, evem you have an alert, the function will go till the end, despite your condition...

And a question... do you really need a separate form for each form object? I think a single form which will include all will do the job...

And, with some definite and few exceptions, the code must be insert in HEAD not in BODY. The code will work in both cases, but is a better position in head, becouse this way you are sure that it will be loaded, as the head is loaded first. I hope I was not too
pedantic... ;)

Now:


<html>
<head>
<title>Wally World Ticket Price Calculator</title>
<script language="javascript">
function calculateCost(){
var AO = 10;
var AS = 20;
var ASR = 30;

if (document.tickettypeForm.tickettype.selectedIndex == 1)
{ticket=AO;}
else if (document.tickettypeForm.tickettype.selectedIndex == 2)
{ticket=AS;}
else if (document.tickettypeForm.tickettype.selectedIndex == 3)
{ticket=ASR;}
else if (document.tickettypeForm.tickettype.selectedIndex == 0)
{alert("You need to select a ticket type first.");return false}
if (document.guesttypeForm.guesttype[0].checked)
{guest=2;}
else if (document.guesttypeForm.guesttype[1].checked)
{guest=1.5;}
else if (document.guesttypeForm.guesttype[2].checked)
{guest=1;}
else
{alert("You need to select a guest type first.");return false}
cost=ticket*guest;
if (document.checkboxForm.checkbox.checked)
{cost=ticket*guest*2;}
alert("The cost of the ticket is $" + cost);
}
</script>
</head>

<body>

<h1>Wally World Ticket Price Calculator</h1>
<br>
Complete the form below to calculate the cost of your ticket.

<form name="tickettypeForm">
Ticket type:
<select name="tickettype" size="1">
<option></option>
<option>Admission only</option>
<option>Admission and all shows</option>
<option>Admission, all shows and unlimited rides</option>
</select>
</form>

<form name="checkboxForm">
Click here if you would like a two-day ticket:
<input type="checkbox" name="checkbox">
</form>
<br>

<form name="guesttypeForm">
Guest type:
<br>
<input type="radio" name="guesttype">Adult<br>
<input type="radio" name="guesttype">Concession<br>
<input type="radio" name="guesttype">Child (under 12)
</form>
<br>

<form>
<input type="button" onClick="return calculateCost()" value="Calculate">&nbsp;<input type="reset">
</form>
</body>
</html>

Kor
10-07-2003, 11:06 AM
oops... I see now the reset don't work, as it refferes to a definite form, not to all. See now why is better (in case you don't need different forms, for sumbit reasons) to use a single form. Here is the correct code, with a small coorection either (make document.forms[0] a variable, as it repeats in code several times):



<html>
<head>
<title>Wally World Ticket Price Calculator</title>
<script language="javascript">
function calculateCost(){
var AO = 10;
var AS = 20;
var ASR = 30;
d = document.forms[0];
if (d.tickettype.selectedIndex == 1)
{ticket=AO;}
else if (d.tickettype.selectedIndex == 2)
{ticket=AS;}
else if (d.tickettype.selectedIndex == 3)
{ticket=ASR;}
else if (d.tickettype.selectedIndex == 0)
{alert("You need to select a ticket type first.");return false}
if (d.guesttype[0].checked)
{guest=2;}
else if (d.guesttype[1].checked)
{guest=1.5;}
else if (d.guesttype[2].checked)
{guest=1;}
else
{alert("You need to select a guest type first.");return false}
cost=ticket*guest;
if (d.checkbox.checked)
{cost=ticket*guest*2;}
alert("The cost of the ticket is $" + cost);
}
</script>
</head>
<body>
<h1>Wally World Ticket Price Calculator</h1>
<br>
Complete the form below to calculate the cost of your ticket.
<form>
Ticket type:
<select name="tickettype" size="1">
<option></option>
<option>Admission only</option>
<option>Admission and all shows</option>
<option>Admission, all shows and unlimited rides</option>
</select>
<br>
<br>
Click here if you would like a two-day ticket:
<input type="checkbox" name="checkbox">
<br>
<br>
Guest type: <br>
<input type="radio" name="guesttype">Adult<br>
<input type="radio" name="guesttype">Concession<br>
<input type="radio" name="guesttype">Child (under 12)
<br>
<br>
<input type="button" onClick="return calculateCost()" value="Calculate">&nbsp;<input type="reset">
</form>
</body>
</html>

Nicole
10-08-2003, 03:10 AM
Kor you are a genius
Thank you thank you thank you

I knew I had major mistakes, but now I can see where I went wrong.
If you are ever in country NSW Australia I'll buy you a beer!!!

This is a great site & I'll be definitely be back, hopefully helping someone else.

Cheers Nicole:) :thumbsup:

Kor
10-08-2003, 09:00 AM
:thumbsup: TX for the beer, consider it already drunk (not me :) , the beer ) even I live in the opposite part of the Globe. I'll be glad to help you,as far as my knowledges let me, anytime. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum