# Thread: checkbox value form help!

1. Registered User
Join Date
Sep 2008
Posts
12

## checkbox value form help!

Hello,
I have been at this forever and cannot find any solution
I have 5 checkboxes, the first 3 each have a value of 260, check 2 or more and the total is maxed out at 390. So far so good.
The problem is the 2 other checkboxes. They each have a value of 130. If checked, their values need to be added to the total no matter what. In other words these 2 checkboxes need to be exempt from the max 390 rule.
I just can't get this to work.

code:

<script type="text/javascript">
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (needle) {
for (var i = 0; i < this.length; i++) {
if (this[i] === needle) return i;
}
return -1;
};
}
// Reference to our form element, and other things
var form = document.forms["form1"], total = checked = 0,
exempt = ["checkbox_group_4[]", "checkbox_group_5[]"];
// Bind up our calculate method when our form changes
} else if (form.attachEvent) {
form.attachEvent("onchange", calculate);
} else {
form.onchange = calculate;
}
// Define our Calculate function
function calculate () {
// Reset total and checked
total = 0, checked = 0;
// Cycle over each item in our form
for (var i = 0; i < form.length; i++) {
// Handle only those that are checked
if (form[i].checked) {
// Add their value to the total value
total += parseInt(form[i].value, 10);
// If they aren't extempt from flatrate
if (exempt.indexOf(form[i].name) < 0) {
// Increment the number of eligible items
checked += 1;

}

}
}
// If more than one eligible item is checked
if (checked > 1) {
total = 390;

// Else if only one eligible item is checked
}
else if (checked > 0) {
total += 0;
}

// Show the user the estimated value
form["total"].value = total;
}

};
</script>

Form:

<form id="contactForm" name="form1" action="send-mail.php" method="POST"><fieldset>

<input type="checkbox" name="checkbox_group_1[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Mardi 19h à 21h30</label> <br/>

<input type="checkbox" name="checkbox_group_2[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Vendredi 19h à 21h30</label>

<input type="checkbox" name="checkbox_group_3[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Vendredi 19h à 21h30</label>

<input type="checkbox" name="checkbox_group_4[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Mardi 19h à 21h30</label> <br/>

<input type="checkbox" name="checkbox_group_5[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Vendredi 19h à 21h30</label>

<input type="text" size="2" name="total" value="0"/>

</fieldset>
</form>

2. Registered User
Join Date
Aug 2013
Posts
6
See my changes in red color.
I use a variable named end in calculate function. if any of last two or both checkboxes are checked it will count that number and where you assigning total. There is loop to add 130 upto that number of times. Hope it will help. thnks

code:

<script type="text/javascript">

// Define our Calculate function
function calculate () {
// Reset total and checked
total = 0, checked = 0, end=0; //define end variable
// Cycle over each item in our form
for (var i = 0; i < form.length; i++) {
// Handle only those that are checked
if (form[i].checked) {
// Add their value to the total value
total += parseInt(form[i].value, 10);
// If they aren't extempt from flatrate
if (exempt.indexOf(form[i].name) < 0) {
// Increment the number of eligible items
checked += 1;

}
else
{
end +=1;
}

}
}
// If more than one eligible item is checked
if (checked > 1) {
total = 390;
for(var i=0; i< end; i++)
{
total +=130;
}

// Else if only one eligible item is checked
}
else if (checked > 0) {
total += 0;
}

// Show the user the estimated value
form["total"].value = total;
}

};
</script>

Form:

<form id="contactForm" name="form1" action="send-mail.php" method="POST"><fieldset>

<input type="checkbox" name="checkbox_group_1[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Mardi 19h à 21h30</label> <br/>

<input type="checkbox" name="checkbox_group_2[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Vendredi 19h à 21h30</label>

<input type="checkbox" name="checkbox_group_3[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Vendredi 19h à 21h30</label>

<input type="checkbox" name="checkbox_group_4[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Mardi 19h à 21h30</label> <br/>

<input type="checkbox" name="checkbox_group_5[]" onclick="calculate()" value="260" class=" " title="Cochez ici" />
<label>Vendredi 19h à 21h30</label>

<input type="text" size="2" name="total" value="0"/>

</fieldset>
</form>

3. Registered User
Join Date
Sep 2008
Posts
12
Hello,
thanks for your help. Something is wrong, for some reason i am not getting any values, total stays at 0.
thanks

4. Registered User
Join Date
Aug 2013
Posts
6
Hi!
I 've verified at my end and it is working fine. Further you can initialize the "end" variable in window.onload function as you did with checked variable. And then before for loop just add the condition that if(end>0).
thanks

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.10646 seconds
• Memory Usage 2,871KB
• Queries Executed 15 (?)
Template Usage (32):
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (4)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (4)postbit
• (4)postbit_onlinestatus
• (4)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_postinfo_query
• fetch_postinfo
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete