Jump to content


JavaScript - Saving all variable and alert the answers


4 replies to this topic

#1 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 09 May 2006 - 07:48 PM

When an user presses "submit survey results", I have to make an alert message saying:

Your Survey answers are:

Preferred transportation method: whatever their choice is
Carpool? whatever their choice is
Travel methods: whatever their choice is
Distance traveled: whatever their choice is
Comments: whatever their choice is

I got the html part done, but i can't seem to get the JavaScript part working, please help me out.
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Transportation Survey</title>
</head>
<script language="JavaScript">
<!--

code

//-->
</script>
<body bgcolor="#FFFFFF">

<p align="left"><font size="5">Commuter Survey</font></p>

<form method="POST" name="frmSurvey">
	<p><br>
	<font size="4">I prefer to commute by: <select
	name="lstTravelChoices" size="2">
		<option SELECTED>Choose one</option>
		<option value="My Own Car">My Own Car</option>
		<option value="Stretch Limo">Stretch Limo</option>
		<option value="Bus">Bus</option>
		<option value="Motorcycle">Motorcycle</option>
		<option value="Train">Train</option>
		<option value="Balloon">Balloon</option>
		<option value="Pickup Truck">Pickup Truck</option>
		<option value="Horse">Horse</option>
		<option value="Other">Other</option>
	</select>
	</font></p>
	<p><font size="4">I am willing to carpool: 
	<input type="radio" checked name="radCarpool" value="Yes">Yes		 <input type="radio" name="radCarpool" value="No">No
	</font></p>
	<p><font size="4">During the last week I traveled by: <input
	type="checkbox" name="chkCar" value="Car">Car <input
	type="checkbox" name="chkBus" value="Bus">Bus <input
	type="checkbox" name="chkWalk" value="Walk">Walk <input
	type="checkbox" name="chkBike" value="Bike">Bike <input
	type="checkbox" name="chkOther" value="Other">Other</font></p>
	<p><font size="4">Distance I travel: 
	<input type="text" size="5" name="boxDistance"	 value="0"></font></p>
	<p><font size="4">Comments: 
	<input type="text" size="47"
	name="boxComments"></font></p>
	<p><font size="4">
<input type="submit" name="btnSubmit"
	value="Submit Survey Results" onClick="DisplayResults();"> 
<input type="reset"
	name="btnReset" value="Reset Answers"></font></p>
</form>
</body>
</html>


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 09 May 2006 - 08:35 PM

I don't see any JavaScript in there, what problems are you having with getting it to work?

To start, i'd recommend moving the DisplayResults() from the button's onClick event, to the form's onSubmit event, and seeing if that changes anything.

#3 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 09 May 2006 - 10:10 PM

I don't know what javascript code to write to save values and make them appear in the alert.

#4 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 10 May 2006 - 08:47 AM

well you can see what he checked
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Transportation Survey</title>
</head>
<body bgcolor="#FFFFFF">
<script language="JavaScript">
<!--
var Travl=new Array('Choose one','My Own Car','Stretch Limo','Bus','Motorcycle','Train','Balloon','Pickup Truck','Horse','Other');
var s='';
function DisplayResults()
{
for(i=0;i<10;i++){
if (document.frmSurvey.lstTravelChoices[i].selected==true) s=s+'Preferred transportation method: '+Travl[i]+'\n';
}
if (document.frmSurvey.radCarpool[0].selected==true) s=s+'Carpool? '+'Yes'+'\n';
else s=s+'Carpool? '+'No'+'\n';
s=s+'Travel methods: ';
if(document.frmSurvey.chkCar.checked==true)s=s+'Car ';
if(document.frmSurvey.chkBus.checked==true)s=s+'Bus ';
if(document.frmSurvey.chkWalk.checked==true)s=s+'Walk ';
if(document.frmSurvey.chkBike.checked==true)s=s+'Bike ';
if(document.frmSurvey.chkOther.checked==true)s=s+'Other ';
s=s+'\n';
s=s+'Distance traveled: '+document.frmSurvey.boxDistance.value+'\n';
s=s+'Comments: '+document.frmSurvey.boxComments.value+'\n';
alert(s);
}
//-->
</script>

<p align="left"><font size="5">Commuter Survey</font></p>

<form name="frmSurvey">
<p><br>
<font size="4">I prefer to commute by: <select
name="lstTravelChoices" size="2">
<option SELECTED>Choose one</option>
<option value="My Own Car">My Own Car</option>
<option value="Stretch Limo">Stretch Limo</option>
<option value="Bus">Bus</option>
<option value="Motorcycle">Motorcycle</option>
<option value="Train">Train</option>
<option value="Balloon">Balloon</option>
<option value="Pickup Truck">Pickup Truck</option>
<option value="Horse">Horse</option>
<option value="Other">Other</option>
</select>
</font></p>
<p><font size="4">I am willing to carpool:
<input type="radio" checked name="radCarpool" value="Yes">Yes <input type="radio" name="radCarpool" value="No">No
</font></p>
<p><font size="4">During the last week I traveled by: <input
type="checkbox" name="chkCar" value="Car">Car <input
type="checkbox" name="chkBus" value="Bus">Bus <input
type="checkbox" name="chkWalk" value="Walk">Walk <input
type="checkbox" name="chkBike" value="Bike">Bike <input
type="checkbox" name="chkOther" value="Other">Other</font></p>
<p><font size="4">Distance I travel:
<input type="text" size="5" name="boxDistance" value="0"></font></p>
<p><font size="4">Comments:
<input type="text" size="47"
name="boxComments"></font></p>
<p><font size="4">
<input type="button" name="btnSubmit"
value="Submit Survey Results" onClick="DisplayResults();">
<input type="reset"
name="btnReset" value="Reset Answers"></font></p>
</form>
</body>
</html>
Use this script right here

#5 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 11 May 2006 - 08:50 AM

thank you very much, you are so skilled in JavaScript. but i am not... i envy you.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users