...

View Full Version : help with basic javascript for uni assignement please! :-)



sjjs1985
12-06-2009, 04:09 PM
hi all, just got to make a basic form for my assignemment but i cant seem to get it to work. please could somebody point me in the right direction? thank you


<head>

<script type="text/javascript">

function make(myform)
{
var name = form.nameinput.value ;
var age = form.ageinput.value ;
var location = form.locationinput.value ;
var z = form.team.selectedIndex ;
var team = form.team.options[z].value;
alert("Thank you " + name + ".Good luck supporting " + team + );
}
</script>

</head>

<body>

<form method="get" action="" name="myform">
<h2>Order form</h2>
<fieldset>
<p>Please enter your details</p>
Name: <input type="text" value="" name="nameinput"/><br/>
Age: <input type="text" value="" size="2" name="ageinput"/><br/>
Location: <input type="text" value="" name="locationinput"/>

<p>Choose your favourite football team</p>
<select name="team">
<option value="arsenal">Arsenal</option>
<option value="aston_villa">Aston Villa</option>
<option value="birmingham_city">Birmingham City</option>
<option value="blackburn_rovers">Blackburn Rovers</option>
<option value="bolton_wanderers">Bolton Wanderers</option>
<option value="burnley">Burnley</option>
<option value="chelsea">Chelsea</option>
<option value="everton">Everton</option>
<option value="fulham">Fulham</option>
<option value="hull">Hull</option>
<option value="liverpool">Liverpool</option>
<option value="manchester_city">Manchester City</option>
<option value="manchester_united">Manchester United</option>
<option value="portsmouth">Portsmouth</option>
<option value="stoke_city">Stoke City</option>
<option value="sunderland">Sunderland</option>
<option value="tottenham_hotspur">Tottenham Hotspur</option>
<option value="west_ham">West Ham</option>
<option value="wigan_athletic">Wigan Athletic</option>
<option value="wolverhampton_wanderers">Wolverhampton Wanderers
</option>
</select>
</fieldset>
<input type="button" onclick="make(this.form)" value="Submit Details" name="button"/>
</form>

Philip M
12-06-2009, 05:02 PM
Check this out:-


<script type="text/javascript">

function make(form) {
var name = form.nameinput.value ;
var age = form.ageinput.value ;
var location = form.locationinput.value ;
var z = form.team.selectedIndex ;
var team = form.team.options[z].value;
alert("Thank you " + name + ". Good luck supporting " + team + );
}
</script>

Bookshop Assistant: "This book about Javascript will save you half your work".
Lazy Student: "Oh good! I'll take two!"

sjjs1985
12-06-2009, 05:24 PM
thanks for the reply but still no luck :(

Philip M
12-06-2009, 05:47 PM
Well, it works fine for me. Check your error console. Did you correct the alert syntax?

BTW, you would do better to change
var team = form.team.options[z].value;
to
var team = form.team.options[z].text;

so that they are thanked for supporting
Blackburn Rovers
rather than
blackburn_rovers

or make the option values the same as the text.

Note that
function make(form) { //opening brace on same line
is to be preferred to
function make(form)
{
to avoid the risk of a fatal error of putting a semi-colon after function make(form)


BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [ code ] and closing [ /code ] tags - omit the spaces. You can (and should) edit your previous post.

randomuser773
12-06-2009, 05:53 PM
hi all, just got to make a basic form for my assignemment but i cant seem to get it to work. please could somebody point me in the right direction?

FireFox -> Tools / Error Console

sjjs1985
12-06-2009, 06:00 PM
my error console says:

Error: syntax error
Source code:
alert("Thank you " + name + ". Good luck supporting " + team + );

and an arrow points to the last bracket.

all help greatly appreciated. thanking you for your time. sam

Philip M
12-06-2009, 06:13 PM
I have already pointed out to you the error:-

alert("Thank you " + name + ". Good luck supporting " + team + );

sjjs1985
12-06-2009, 06:22 PM
ah stupid me. i didnt realise your red font meant it shouldnt be there! thanks all for the help!! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum