Current location: Hot Scripts Forums » General Web Coding » JavaScript » Updating current money spent in a form with checkboxes. . .


Updating current money spent in a form with checkboxes. . .

Reply
  #1 (permalink)  
Old 07-12-05, 03:30 PM
Spreegem Spreegem is offline
Newbie Coder
 
Join Date: Jan 2004
Location: In front of my computer
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Updating current money spent in a form with checkboxes. . .

I have a form with prices of items that PHP displays, and the items have check boxes next to them so that you can select or deselect them, and I was wondering how I would update the current cost when a user checks or unchecks a checkbox?
Thanks in Advance
Reply With Quote
  #2 (permalink)  
Old 07-12-05, 08:00 PM
sszettella sszettella is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
You might hook a javascript function into the onclick event of the checkbox. When the checkbox is clicked, you can see what the state is, then add or subtract accordingly. This code should help, it's a working example:
Code:
<html>
<head>
<script language="javascript">
function changePrice(byAmount, state) {
  var curVal = document.forms[0].total.value;

  if (state) {  // go up
    var newVal = new Number(curVal)+new Number(byAmount);
  } else {  // go down
    var newVal = new Number(curVal)-new Number(byAmount);
  }

  document.forms[0].total.value=newVal;
}
</script>
</head>
<body>
<form>
Check to add 10: <input type="checkbox" name="cb1" onclick="javascript:changePrice(10, this.checked)"/><br>
Check to add 20: <input type="checkbox" name="cb2" onclick="javascript:changePrice(20, this.checked)"/><br>
(unchecking the boxes subtracts)<br>
<input type="text" name="total" value="0"/>
(you can also put in your own value here, then use the checkboxes to change it)


</form>
</body>
</html>
__________________
Steve Szettella
4word systems
http://www.4wordsystems.com
http://szettella.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 07-12-05, 08:02 PM
sszettella sszettella is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
By the way, this example is using an <input type="text">. . . however that is not required. You can actually change the value of virtually any text on the screen. The example is just the simplest way to implement what you've described.
__________________
Steve Szettella
4word systems
http://www.4wordsystems.com
http://szettella.blogspot.com
Reply With Quote
  #4 (permalink)  
Old 07-13-05, 09:09 PM
Spreegem Spreegem is offline
Newbie Coder
 
Join Date: Jan 2004
Location: In front of my computer
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Thank You, that's exactly what I was looking for.
Reply With Quote
  #5 (permalink)  
Old 07-13-05, 09:10 PM
sszettella sszettella is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Your welcome. Glad to help.
__________________
Steve Szettella
4word systems
http://www.4wordsystems.com
http://szettella.blogspot.com
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 with updating a multiple checkbox form to a MySQL DB jimb575 PHP 1 01-31-06 08:04 PM
Checkboxes on form = mailto recipient. Bojon PHP 4 12-21-04 06:07 PM
asp: checkboxes & multi-page form seala ASP 0 09-02-03 01:58 PM


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