Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need to multiply a checkbox value.

    I am stuck in a rut with javascript. Here is the page that I am working on:

    https://www.puc.edu/PUC/newsevents/g...der_form.shtml

    It is for an order form, which was working great. I had it so the user could select which DVD's they wanted to order, and it would total the amout of each DVD by what checkbox they select. But now, I realized I need to have a field for number of orders per DVD. Which throws in a whole new problem. I now need to have the DVD total come up in the total box, but if someone enters that they need 5 DVD, I need the total field to multiply by the number entered. I am still new to this, so if anyone could look at my code, and give me a few times it would be much appreciated!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    function Cal(obj){
     var frm=obj.form;
     if (!window[frm.name+'Ary']&&obj.type=='text'){ window[frm.name+'Ary']=[]; }
     if (!window[obj.name+'Set']&&obj.type=='text'){ window[obj.name+'Set']==true; window[frm.name+'Ary'].push(obj); }
     if (!window[frm.name+'Ary']){ return; }
     var ary=window[frm.name+'Ary'];
     var eles=frm.elements;
     var val=0
     for (var zxc0=0;zxc0<eles.length;zxc0++){
      for (var zxc1=0;zxc1<ary.length;zxc1++){
       if (eles[zxc0]==ary[zxc1]){
        if (eles[zxc0-1].checked&&eles[zxc0].value.length>0&&!isNaN(eles[zxc0].value)){
         val+=eles[zxc0-1].value*eles[zxc0].value;
        }
       }
      }
     }
     frm.hiddentotal.value=val;
    }
    
    
    /*]]>*/
    </script>
    <form name="AnyName" >
    <table>
    
                              <tr>
                                <td colspan="2"><p><span class="sans11"><strong><font color="#FF0000" size="2"><strong>*</strong></font>Please select which DVDs you would like to order: </strong></span></p>
                                  <p><span class="sans11">Full Weekend Packages:</span> <span class="sans11"><font size="2"><B><BR>
                                    </B></font></span>
                                      <input name="Consecration, Baccalaureate, and Commencement" type="checkbox" id="Consecration, Baccalaureate, and Commencement"   value=50.00   onclick="Cal(this);" >
                                      <span class="sans11">Consecration, Baccalaureate, and Commencement  ($50) x
                                      <input name="Consecration, Baccalaureate, and Commencement Quantity" type="text" class="boxcolor" id="Consecration, Baccalaureate, and Commencement Quantity" size="2" onblur="Cal(this);">
    
    </span> <span class="sans11"><font size="2"><B><BR>
                                        </B></font></span>
                                      <input name="Consecration, Baccalaureate, Nurses Pinning and Commencement" type="checkbox" id="Consecration, Baccalaureate, Nurses Pinning and Commencement"   value=65.00 onclick="Cal(this);" >
                                    <span class="sans11">Consecration, Baccalaureate, Nurses' Pinning and Commencement  ($65) </span> <span class="sans11">x
                                    <input name="Consecration, Baccalaureate, Nurses Pinning and Commencement Quantity" type="text" class="boxcolor" id="Consecration, Baccalaureate, Nurses Pinning and Commencement Quantity" size="2"  onblur="Cal(this);">
                                    </span></p>
                                  <p><span class="sans11">Single Event:</span> <span class="sans11"><font size="2"><B><BR>
    
                                  </B></font></span>
                                      <input name="Baccalaureate" type="checkbox" id="Baccalaureate"   value=20.00 onclick="Cal(this);" >
                                      <span class="sans11">Baccalaureate DVD ($20) x
                                      <input name="Baccalaureate Quantity" type="text" class="boxcolor" id="Baccalaureate Quantity" size="2" onblur="Cal(this);" >
                                      </span> <span class="sans11"><font size="2"><B><BR>
                                        </B></font></span>
                                      <input name="Consecration" type="checkbox" id="Consecration"   value=20.00 onclick="Cal(this);" >
                                      <span class="sans11">Consecration DVD ($20) x
                                      <input name="Consecration Quantity" type="text" class="boxcolor" id="Consecration Quantity" size="2" onblur="Cal(this);">
    
                                      </span> <span class="sans11"><font size="2"><B><BR>
                                        </B></font></span>
                                      <input name="Commencement" type="checkbox" id="Commencement"   value=20.00 onclick="Cal(this);" >
                                      <span class="sans11">Commencement DVD ($20) x
                                      <input name="Commencement Quantity" type="text" class="boxcolor" id="Commencement Quantity" size="2" onblur="Cal(this);">
                                      <font size="2"><b><br>
                                        </b></font></span>
                                      <input name="Nurses Pinning" type="checkbox" id="Nurses Pinning"   value=20.00 onclick="Cal(this);" >
                                      <span class="sans11">Nurses' Pinning DVD ($20) x
                                      <input name="Nurses Pinning Quantity" type="text" class="boxcolor" id="Nurses Pinning Quantity" size="2" onblur="Cal(this);" >
    
                                      <font size="2"><b><br>
                                          </b></font><font size="2"><b><br>
                                      </b></font></span></p>
    								  	<input type="text" name="hiddentotal" value=0>
    	                                <input type="hidden" name="hiddenpriorradio" value=0>
    </td>
                              </tr>
                            </table>
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Seems like that code is a little off. See here:

    https://www.puc.edu/PUC/newsevents/g...er_form1.shtml

    It seems to act sort of speratic, and does not do that math correcly. Did I do something wrong?

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    your page tested and works fine, calulating on the onblur event

    + thank you is good
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #5
    Regular Coder
    Join Date
    May 2003
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Still not working. The calculations are wrong. It seems that it is double the number that it is suppose to be. Also, if you check back between the quantaty fields, it keeps adding up the total, for no reason! Is there a way to correct this?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •