Show price in field when radio button or checkbox is selected.

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: May 2008
Posts: 15
Reputation: jrconstance is an unknown quantity at this point 
Solved Threads: 0
jrconstance jrconstance is offline Offline
Newbie Poster

Show price in field when radio button or checkbox is selected.

 
0
  #1
Jun 19th, 2009
I am working on a registration form for a conference. The form has several sections of radio buttons and checkboxes and I would like to have it set up so that if a radio button or checkbox is selected the price related to that selection shows up in the rigjt column of the form and can then be totaled up at the bottom of the page.

So, looking for assistance with the javascript (assuming javascript is the best way to do this) for:

1. Show price in field.
2. Total up all of the prices at the bottom of the page.

Any help would be greatly appreciated.

You can view the registration form at:

http://asclsregionviii.org/wpi/imss_09_reg.php

J.R. Constance
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is online now Online
Posting Shark

Re: Show price in field when radio button or checkbox is selected.

 
0
  #2
Jun 20th, 2009
You could try this code:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8" standalone="no"?>
  2. <?xml-stylesheet type="text/css" href="#css21" media="screen"?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5. <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <head profile="http://www.w3.org/2005/10/profile">
  7. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  8. <meta http-equiv="Window-target" content="_top" />
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10. <meta http-equiv="Content-Style-Type" content="text/css" />
  11. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  12. <title>Free Live Help!</title>
  13. <style id="css21" type="text/css" media="screen">
  14. /* <![CDATA[ */
  15. table {
  16. border : none;
  17. border-collapse : collapse;
  18. color : #708090;
  19. width : 80%; }
  20. th {
  21. padding : .300em;
  22. border-bottom : 1px solid #aaa;
  23. border-top : 1px solid #aaa; }
  24. td {
  25. border-bottom : 1px solid #aaa;
  26. background-color : #f5f5f5;
  27. padding : .200em;
  28. letter-spacing : 3px;
  29. text-align : center;
  30. vertical-align : middle; }
  31. /* ]]> */
  32. </style>
  33. <script id="javascript1.5" type="text/javascript">
  34. // <![CDATA[
  35. var showPrice = function() {
  36. var numspan = (( "getElementsByTagName" in document ) ? document.getElementsByTagName("span") : document.all.tags("span") );
  37.  
  38. var ids = this.id.match(/\d+/)[ 0 ];
  39. var span = (( "getElementById" in document ) ? document.getElementById("tot" + ids ) : document.all[ "tot" + ids ] );
  40. var overallAmount = (( "getElementById" in document ) ? document.getElementById("overall") : document.all.overall );
  41. span.innerHTML = "";
  42. var amt = this.value * 1;
  43. var qty = (( "getElementById" in document ) ? document.getElementById("item" + ids ) : document.all[ "item" + ids ] ).value *1;
  44. var total = "";
  45. total = (( !qty ) ? 0 : (( qty ) * amt ));
  46.  
  47. if ( this.checked && qty ) {
  48. span.innerHTML = "<span style=\"color : #E00;\">" + total + "</span>";
  49. overallAmount.innerHTML = (( numspan["tot0"].innerText * 1 ) + numspan["tot1"].innerText * 1 );
  50. return true; }
  51. alert( "Please specify the quantity of selected ( item #" + (( ids * 1 ) + 1 ) + " )." );
  52. this.checked = false;
  53. return false;
  54. };
  55.  
  56. var products = function() {
  57. var form;
  58. try {
  59. form = document.forms.testform;
  60. } catch( e ) {
  61. form = (( "getElementById" in document ) ? document.getElementById("testform") : document.all.testform );
  62. }
  63. var oLen = form.elements.length;
  64. for ( var x = 0; x < oLen; x++ ) {
  65. if ( "addEventListener" in window ) {
  66. if ( form.elements[ x ].type === "checkbox" ) {
  67. form.elements[ x ].addEventListener( "click", showPrice, false );
  68. continue
  69. }
  70. } else if ( "attachEvent" in window ) {
  71. if ( form.elements[ x ].type === "checkbox" ) {
  72. form.elements[ x ].attachEvent( "onclick", showPrice );
  73. continue
  74. }
  75. } if ( form.elements[ x ].type === "checkbox" ) {
  76. form.elements[ x ].onclick = showPrice;
  77. continue;
  78. }
  79. }
  80. };
  81.  
  82. window.onload = products;
  83. // ]]>
  84. </script>
  85. </head>
  86. <body>
  87. <div id="main">
  88. <form id="testform" action="#" onsubmit="return false;">
  89. <table id="testtable" frame="void" rules="none" summary="Javascript :: Live Demo!">
  90. <tr>
  91. <th>Add</th><th>Item</th><th>Qty.</th><th>Amount</th></tr>
  92. <tr>
  93. <td><input type="checkbox" id="chb0" name="chb0" value="1.25" /></td>
  94. <td>Sample Item #1</td>
  95. <td><input type="text" id="item0" name="item0" value="" size="6" maxlength="5" /></td>
  96. <td><span id="tot0"></span></td>
  97. </tr>
  98. <tr>
  99. <td><input type="checkbox" id="chb1" name="chb1" value="3.25" /></td>
  100. <td>Sample Item #2</td>
  101. <td><input type="text" id="item1" name="item1" value="" size="6" maxlength="5" /></td>
  102. <td><span id="tot1"></span></td>
  103. </tr>
  104. <tr>
  105. <td style="background-color : #fff; border : none;"></td>
  106. <td style="background-color : #fff; border : none;"></td>
  107. <td style="background-color : #fff; border : none;"></td>
  108. <td style="background-color : #fff; border : none;"><span id="overall" style="color : #E00; font-weight : bold; border : none;"></span></td>
  109. </tr>
  110. </table>
  111. </form>
  112. </div>
  113. </body>
  114. </html>

hope it helps...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 15
Reputation: jrconstance is an unknown quantity at this point 
Solved Threads: 0
jrconstance jrconstance is offline Offline
Newbie Poster

Re: Show price in field when radio button or checkbox is selected.

 
0
  #3
Jun 20th, 2009
Thanks. This works well for the checkbox that I have that has a quantity.

A couple more questions, though:

1. How to do this if the quantity is always going to be 1. Wouldn't need to have a field to add quantity.

2. How to do this for an array of radio buttons where the $$ value would be different for each radio button.

Thx
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is online now Online
Posting Shark

Re: Show price in field when radio button or checkbox is selected.

 
0
  #4
Jun 20th, 2009
It depends' in the concept of your needs.

Try to post a short form sample code, including some of the elements you need that will take part in the entire form processing including detailed instructions.

Then i'll handle all the coding into it.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 15
Reputation: jrconstance is an unknown quantity at this point 
Solved Threads: 0
jrconstance jrconstance is offline Offline
Newbie Poster

Re: Show price in field when radio button or checkbox is selected.

 
0
  #5
Jun 20th, 2009
Thanks.

OK, first off, this is a php page with a form that gathers information that is written to a MySQL database. All buiilt using Adobe Dreamweaver Developer's Toolbox. That php/MySQL functionality all works and everything writes to the db as required.

Here is a sample of the code:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
  2. <table width="720" cellpadding="2" cellspacing="0" class="KT_tngtable">
  3. <tr>
  4. <td class="KT_th">3-Day Package:</td>
  5. <td>Includes all general sessions, all exhibit events, lunches, and
  6. afternoon Breakout Sessions on Thursday, Friday, and Saturday.
  7. <td> </tr>
  8. <tr>
  9. <td width="180" class="KT_th"><label for="reg3day_1"></label></td>
  10. <td><div>
  11. <input <?php if (!(strcmp(KT_escapeAttribute($row_rsregistrant['reg3day']),"ASCLS Member"))) {echo "CHECKED";} ?> type="radio" name="reg3day" id="reg3day_1" value="ASCLS Member" />
  12. <label for="reg3day_1">ASCLS Professional I or II Member ($210.00)</label>
  13. </div>
  14. <div>
  15. <input <?php if (!(strcmp(KT_escapeAttribute($row_rsregistrant['reg3day']),"ASCLS Collaborative"))) {echo "CHECKED";} ?> type="radio" name="reg3day" id="reg3day_2" value="ASCLS Collaborative" />
  16. <label for="reg3day_2">ASCLS Collaborative Member ($315.00)</label>
  17. </div>
  18. <div>
  19. <input <?php if (!(strcmp(KT_escapeAttribute($row_rsregistrant['reg3day']),"ASCLS Student/Emeritus"))) {echo "CHECKED";} ?> type="radio" name="reg3day" id="reg3day_3" value="ASCLS Student/Emeritus" />
  20. <label for="reg3day_3">ASCLS Student/Emeritus Member ($140.00)</label>
  21. </div>
  22. <?php echo $tNGs->displayFieldError("registrant", "reg3day"); ?>
  23. <td>
  24. </tr>
  25. <tr>
  26. <td width="180" class="KT_th"><label for="regExhibits">Exhibits Only:</label></td>
  27. <td><input <?php if (!(strcmp(KT_escapeAttribute($row_rsregistrant['regExhibits']),"Y"))) {echo "checked";} ?> type="checkbox" name="regExhibits" id="regExhibits" value="Y" />
  28. Entrance Fee - One Time Pass, includes food ($25.00) <?php echo $tNGs->displayFieldError("registrant", "regExhibits"); ?> </td>
  29. <td>&nbsp;</td>
  30. </tr>
  31. </table>
  32. <input type="hidden" name="regLate" id="regLate" value="<?php echo KT_escapeAttribute($row_rsregistrant['regLate']); ?>" />
  33. </form>

This includes one array of radio buttons (althought there are several sections of these on the complete form), and also one example of a checkbox that has a $$ value but not a quantity.

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 15
Reputation: jrconstance is an unknown quantity at this point 
Solved Threads: 0
jrconstance jrconstance is offline Offline
Newbie Poster

Re: Show price in field when radio button or checkbox is selected.

 
0
  #6
Jun 20th, 2009
In looking at what I submitted I realized I had left off the submit function for the form. Here is the correct code for the last few lines of the form.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <tr class="KT_buttons">
  2. <td colspan="3"><input type="submit" name="KT_Insert1" id="KT_Insert1" value="Insert record" /> </td>
  3. </tr>
  4. </table>
  5. <input type="hidden" name="regLate" id="regLate" value="<?php echo KT_escapeAttribute($row_rsregistrant['regLate']); ?>" />
  6. </form>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is online now Online
Posting Shark

Re: Show price in field when radio button or checkbox is selected.

 
0
  #7
Jun 20th, 2009
Ok i'll post back your solution later, it's a bit getting late here...

essential
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 15
Reputation: jrconstance is an unknown quantity at this point 
Solved Threads: 0
jrconstance jrconstance is offline Offline
Newbie Poster

Re: Show price in field when radio button or checkbox is selected.

 
0
  #8
Jun 20th, 2009
Thanks so much.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is online now Online
Posting Shark

Re: Show price in field when radio button or checkbox is selected.

 
0
  #9
Jun 20th, 2009
Hi there,

You can thanked me when we finished this.

If you need some quick brief regarding about getting all the values specified in your radio buttons, then you might want to check back on the showPrice() function provided in my recent sample code.
It deals on the same process and the only difference is the type's of element that we are getting.

You can easily referenced form elements using this method:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var form = document.forms[ /* index or id will work here */ ];
  2.  
  3. for ( var x = 0; x < form.elements.length; x++ ) { // Looping throughout the entire form elements.
  4. if ( form.elements[ x ].type === 'radio' ) { // Let's check/get if this type of element belongs to the radio class.
  5. form.elements[ x ].onclick = function() { // Break the loop and get those radio types'
  6. alert( "The value of this radio button is, " + this.value ); // Alerting its value.
  7.  
  8. // you may apply more things from here.
  9. }; // Closing function.
  10. continue; // continues the loop.
  11. } // You can get other form elements from here. But not getting the radio types.
  12. // Coz it was filtered inside the 1st if() block statement.
  13. alert( form.elements[ x ].type );
  14. } // End Loop

hope it does explain things clearly, knowing that i am not good on explaining things...
Last edited by essential; Jun 20th, 2009 at 4:07 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 15
Reputation: jrconstance is an unknown quantity at this point 
Solved Threads: 0
jrconstance jrconstance is offline Offline
Newbie Poster

Re: Show price in field when radio button or checkbox is selected.

 
0
  #10
Jun 22nd, 2009
I can certainly follow the code, and see what you're doing, but I doubt that I would be able to take this and make it work on my own. I just don't have the experience with javascript to be able to do that.

J.R.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC