Current location: Hot Scripts Forums » General Web Coding » JavaScript » Huge Javascript question and help...


Huge Javascript question and help...

Reply
  #1 (permalink)  
Old 10-02-04, 05:52 PM
CBRyder13 CBRyder13 is offline
Newbie Coder
 
Join Date: Oct 2004
Location: Pennsylvania
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Question Huge Javascript question and help...

Hello everyone. I am building a website for my current computer business. I have been spending countless hours of research trying to figure out how to make a form where selecting one option affects something else (such as a price) on the page. A perfect example of this can be found HERE

As you can see by following that link, the price on the bottom of the page changes depending on what options you select, or deselect. That is basically what I am trying to accomplish on my website.

I heard from somewhere this can be accomplished by using javascript. I am not too experianced with javascript so any help what so ever would be great. If possible, could someone help me with this or steer me in the right direction?

Last edited by CBRyder13; 10-02-04 at 06:40 PM.
Reply With Quote
  #2 (permalink)  
Old 10-04-04, 12:47 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
This is very easy to do.
When you change to another option on a selectbox, an event called onChange is fired. You can set this event to do what you want (call a JavaScript function for example).

Here's a basic example
Code:
<script>
function Recount(){
  var sum=0
  for(i=1;i<=2;i++){
    var box=document.getElementById('box'+i)
    sum+=parseFloat(box.options[box.selectedId].value)
  }
  document.getElementById('total').value=sum
}
</script>

<form>
<select id="box1" onchange="Recount()">
<option value=100>Item 1
<option value=200>Item 2
</select><p>
<select id="box2" onchange="Recount()">
<option value=500>Item 1
<option value=700>Item 2
</select><p>
<input type="text" value="total">
</form>
Reply With Quote
  #3 (permalink)  
Old 10-04-04, 05:47 AM
CBRyder13 CBRyder13 is offline
Newbie Coder
 
Join Date: Oct 2004
Location: Pennsylvania
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
That code doesn't work.

Oh, I have a quick question. If I were to search on google for this, what would I call it? What would I type in google to get results?

Last edited by CBRyder13; 10-04-04 at 05:51 AM.
Reply With Quote
  #4 (permalink)  
Old 10-04-04, 08:05 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, I just wrote it of the top of my head before school started this morning. I had no time to test it so I made a few mistakes...
Here's how it should look:

Code:
<html>
<head>
</head>
<body>
<script>
function Recount(){
  var sum=0
  for(i=1;i<=2;i++){
    var box=document.getElementById('box'+i)
    sum+=parseFloat(box.options[box.selectedIndex].value)
  }
  document.getElementById('total').value=sum
}
</script>

<form>
<select id="box1" onchange="Recount()">
<option value=100 selected>Item 1
<option value=200>Item 2
</select><p>
<select id="box2" onchange="Recount()">
<option value=500 selected>Item 1
<option value=700>Item 2
</select><p>
<input type="text" id="total">
</form>
</body>
</html>
Anyway, I meant it just as an example to show you where to start looking for solutions to your problem
Reply With Quote
  #5 (permalink)  
Old 10-16-04, 02:16 PM
CBRyder13 CBRyder13 is offline
Newbie Coder
 
Join Date: Oct 2004
Location: Pennsylvania
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, that works and i've made it how I want. But now when when I add a submit and reset button and have it set up to use formmail it doesn't send the information with it. It just sends the submit: submit to the email its supposed to email the information to. Basically it works fine, but the only thing is that it doesn't send whats in the drop-down menus or the price. What do I need to do so it sends all the information in the form?

This is what I have.... http://www.hypertecpc.com/custompc.html
That one works fine

This is what is not working.... http://www.hypertecpc.com/xxxcustompc.html
It works, but its not sending ALL the information.

Last edited by CBRyder13; 10-16-04 at 02:21 PM.
Reply With Quote
  #6 (permalink)  
Old 10-28-04, 02:46 PM
CBRyder13 CBRyder13 is offline
Newbie Coder
 
Join Date: Oct 2004
Location: Pennsylvania
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Fixed, but yet another question

I solved the problem I was having about nothing sending when clicking the submit button. Now I have another problem with that script you shared with me above.

When I add more than 2 drop-down menu's, the "total" price doesn't work with the extra drop-down fields. As you can see at http://68.82.122.28/custompc.com I have the two drop down fields there. They work and the total price shows up in the button. When I add another drop-down menu and make it box3 and do the necessary changes, it acts as though it adds $0.00 to the current price or its just not working.

If anyone knows a solution to this please help me out. Thanks!
Reply With Quote
  #7 (permalink)  
Old 10-29-04, 02:27 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
The link you posted doesn't work but I assume it should be .html instead och .com.
To me it looks like it's working. The price changes correctly when I change one of the top 3 selectboxes....
Reply With Quote
  #8 (permalink)  
Old 02-10-05, 11:04 PM
CBRyder13 CBRyder13 is offline
Newbie Coder
 
Join Date: Oct 2004
Location: Pennsylvania
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, I know it's been awhile, but....
For some of the prices I have, and depending on what is selected, the price may look like this, for example: $1673.833373848373

In that code, what could be done to make it round to the nearest hundredths?

Thanks
Reply With Quote
  #9 (permalink)  
Old 02-11-05, 04:54 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Yes. You just have to do something like
Code:
document.getElementById('total').value=Math.round(sum*100)/100
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Newbie Question re: Variables jenf PHP 2 06-20-04 01:04 AM
MySQL question Need Help With something! bearslife PHP 1 05-20-04 09:22 AM
Question (Help) forest496 PHP 1 10-07-03 06:52 PM
MySQL with PHP question. HELP for a newbie kenfused PHP 3 08-02-03 12:53 AM
Question with chown - Change file owner ! Help pls kevin PHP 2 07-09-03 04:36 AM


All times are GMT -5. The time now is 10:31 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.