473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Javascript form data computations

My friend and I are trying to start a business and are writing a website of our own. We have been trying to create an online estimator and are having trouble with writing the javascript that is to handle the form data. I am having trouble capturing the form data for use using the onsubmit operator. We have written all of the options using list boxes using numbers as the values. I have attempted to create a user defined function to caculate the total and I have tried to use an alert box to give the instant result to the user before submitting the info to us. However, I am not sure that I am using the correct syntax to capture the value of the list boxes. Also, I am not sure that my user defined function is written properly. In fact, I can't even get my function to return an alert box even after inserting a if-else statement with alert boxes as both actions. I have to admit that I am new to Javascript (I learned it this weekend) and this is my 1st real project. I could really use some help here. Also, where can I find out how to write the server-side javascript needed for processing data after the submission. Below I have included a smaller version of the code that I wrote. PLEASE HELP!!!

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.     <title>Online Estimator</title>
  4.         <!--
Expand|Select|Wrap|Line Numbers
  1.     <script language="Javascript" type="text\javascript">
  2.     function formTotal () {
  3.        var ourForm = document.forms[0];
  4.        var aquariumIdx = ourForm.aquarium.selectedIndex;
  5.        var aquarium = ourForm.aquarium.options[aquariumIdx].value;
  6.        var tableIdx = ourForm.table.selectedIndex;
  7.        var table = ourForm.table.options[tableIdx].value;
  8.        var bedIdx = ourForm.bed.selectedIndex;
  9.        var bed = ourForm.bed.options[bedIdx].value;
  10.        var couchIdx = ourForm.couch.selectedIndex;
  11.        var couch = ourForm.couch.options[couchIdx].value;
  12.        var chairsIdx = ourForm.chairs.selectedIndex;
  13.        var chairs = ourForm.chairs.options[chairsIdx].value;
  14.        var milesIdx = ourForm.miles.selectedIndex;
  15.        var miles = ourForm.miles.options[milesIdx].value;
  16.        var longWalkIdx = ourForm.longWalk.selectedIndex;
  17.        var longWalk = ourForm.longWalk.options[longWalkIdx].value;
  18.        var stairflightsIdx = ourForm.stairflights.selectedIndex;
  19.        var stairflights = ourForm.stairflights.options[stairflightsIdx].value;
  20.        var preTotal = aquarium + table + bed + couch + chairs + miles;
  21.        var preTotal2 = preTotal * longWalk;
  22.        var Total = preTotal2 * stairflights;
  23.        return = alert (Total)
  24.        }
  25.     </script>
Expand|Select|Wrap|Line Numbers
  1.         -->
  2.      </head>
  3.      <body>
  4.        <h1><b>Online Esitmator</b></h1>
  5.        <form action = "#" onsubmit = "return formTotal()">
  6.  
  7.  
  8. <b>Aquariums:</b>
  9.        <select name = "aquarium">
  10.        <option value = "0">0</option>
  11.        <option value = "3">1</option>
  12.        <option value = "6">2</option>
  13.        <option value = "9">3</option>
  14.        </select>
  15.  
  16.  
  17. <b>Tables:</b>
  18.        <select name = "table">
  19.        <option value = "0">0</option>
  20.        <option value = "6">1</option>
  21.        <option value = "12">2</option>
  22.        <option value = "18">3</option>
  23.        <option value = "24">4</option>
  24.        </select>
  25.  
  26.  
  27.        <b>Beds:</b>
  28.        <select name = "bed">
  29.        <option value = "0">0</option>
  30.        <option value = "10">1</option>
  31.        <option value = "20">2</option>
  32.        <option value = "30">3</option>
  33.        <option value = "40">4</option>
  34.        </select>
  35.  
  36.  
  37.        <b>Couches:</b>
  38.        <select name = "couch">
  39.        <option value = "0">0</option>
  40.        <option value = "10">1</option>
  41.        <option value = "20">2</option>
  42.        <option value = "30">3</option>
  43.        <option value = "40">4</option>
  44.        </select>
  45.  
  46.  
  47.        <b>Chairs:</b>
  48.        <select name = "chairs">
  49.        <option value = "0">0</option>
  50.        <option value = "3">1</option>
  51.        <option value = "6">2</option>
  52.        <option value = "9">3</option>
  53.        <option value = "12">4</option>
  54.        <option value = "15">5</option>
  55.        </select>
  56.  
  57.  
  58.        <b>Miles between Locations:</b>
  59.        <select name = "miles">
  60.        <option value = "5">Under 10</option>
  61.        <option value = "10">10-20</option>
  62.        <option value = "15">20-30</option>
  63.        <option value = "20">30-40</option>
  64.        </select>
  65.  
  66.  
  67.        <b>Long Walk:</b>
  68.        <select name = "longWalk">
  69.        <option value = "1">No</option>
  70.        <option value = "1.2">From Only</option>
  71.        <option value = "1.2">To Only</option>
  72.        <option value = "1.4">To and From</option>
  73.        </select>
  74.  
  75.  
  76.        <b>Flights of Stairs:</b>
  77.        <select name = "stairflights">
  78.        <option value = "1">None</option>
  79.        <option value = "1.2">1 flight</option>
  80.        <option value = "1.4">2 flights</option>
  81.        <option value = "1.6">3 flights</option>
  82.        <option value = "1.8">4 flights</option>
  83.        <option value = "2.0">5 flights</option>
  84.        <option value = "2.2">6 flights</option>
  85.        </select>
  86.  
  87.  
  88.  
  89.        <input type = "submit" value = "Get Estimate!">
  90.  
  91.  
  92.        </form>
  93.     </body>
  94. </html>
  95.  
If anyone can give some insight, it would be greatly appreciated...
-JOE-
Sep 4 '07 #1
4 1672
pbmods
5,821 Expert 4TB
Heya, Joe. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

Try putting the <!-- --> inside of your script block, and it's "text/javascript", not "text\javascript":
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. // <!--
  3.     ...
  4. // -->
  5. </script>
  6.  
Also, change this line:
Expand|Select|Wrap|Line Numbers
  1.  return = alert (Total)
  2.  
To this:
Expand|Select|Wrap|Line Numbers
  1. alert(Total);
  2. return Total;
  3.  
Sep 4 '07 #2
phvfl
173 Expert 100+
Hi Joe,

Welcom to TSDN...

Before I start could you please make sure that you use code tags when entering code into posts as it makes it easier to read.

Secondly good attempt for your first proper code, there are just a few small issues that prevented this form working.

Firstly the HTML comments that you had used were around the script tag instead of inside it, this is why you couldn't get any alerts to work, using HTML comments to hide javascript from browsers that do not understand javascript is good practice, the syntax is:
[HTML]
<script>
<!--
javascript
//-->
</script>
[/HTML]
Another thing is to use a javascript comment before the HTML closing comment. This prevents the closing HTML comment being parsed by the script engine.

The second issue is that in the type attribute you used a backwards slash instead of a forwards one, this prevented the script tag from being understood as javascript, this would make the declaration:

[HTML]
<script type="text/javascript">
<!--
javascript
//-->
</script>
[/HTML]
Now we are onto the script itself. The type of the variable being returned from the selects on the lines like:

Expand|Select|Wrap|Line Numbers
  1. var table = ourForm.table.options[tableIdx].value;
  2.  
is a string. This means that when you were trying to add the values you were concatenating strings instead of adding numbers, this is easy to rectify by multiplying the value by 1 to force it to a number:

Expand|Select|Wrap|Line Numbers
  1. var table = ourForm.table.options[tableIdx].value * 1;
  2.  
Finally, when you are setting the onsubmit to use return the return value should be true or false. Returning true would allow the form to continue and submit, returning false would stop the form from submitting. Place your alert before the return and return a boolean value.

Expand|Select|Wrap|Line Numbers
  1. alert("message text");
  2. return true;
  3.  
If you are always going to submit the form then don't return a value and set the onsubmit action just to the name of the form and don't use the return keyword in either the onsubmit or the function.

I think that this should let your code work. If it doesn't post your revised code and I'll look at what you then have.
Sep 4 '07 #3
I just wanted to thank you guys for the help with this script. Your advice was pure gold. Immediately the code worked after making the changes you suggested. I appreciate the help. I will make sure that my next posted question will have the <tags> you requested (although I will have to figure out what you mean). Thanks again. \


PS--Javascript is cool!
Sep 4 '07 #4
phvfl
173 Expert 100+
I just wanted to thank you guys for the help with this script. Your advice was pure gold. Immediately the code worked after making the changes you suggested. I appreciate the help. I will make sure that my next posted question will have the <tags> you requested (although I will have to figure out what you mean). Thanks again. \


PS--Javascript is cool!
Javascript is indeed cool. Code tags are explained here
Sep 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
14
by: horos | last post by:
hey all, I'm a heavy perl user, not so much a java script user, and was wondering... perl has an extremely nice utility called Data::Dumper, which allows you to dump out the contents of an...
7
by: Bruno Alexandre | last post by:
Hi guys, Sorry about the off topic, but I can't find the ASP group just the ASP dot NET If I want to block a user to change a form after submiting, I add a function that will disable the...
4
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
3
by: anthonybrough | last post by:
I have an asp page that has a form to collect user data in a form. when the user clicks submit the input is validated. If any fields are not acceptable the user clicks on a button to go back to...
78
by: Jeremy J Starcher | last post by:
(Request for Discussion) I've put together a guide that I hope will help novice coders avoid the same hair pulling that I went through. I'm open for comments about it. Have I missed the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.