Solved

counting checkboxes in javascript and passing to javascript function and php action

Posted on 2009-05-20
4
381 Views
Last Modified: 2012-05-07
I am building a form that lets the user check off a matrix of checkboxes, each which to indicate a location/category seleciton. I then want to pass the checked ones (location/category) to a javascript function to list and call PayPal, and then to a php script for subsequent entry into a database. The checkboxes arrays are created dynamically with php, as indicated in the sample code. Each has an onclick event that calls a funciton which increments a global counter if a box is checked, and decrements it if it is unchecked.

The questions are: 1) I am a bit confused on the syntax for calling the increment/decrement function - the array part is messing me up. 2) Once the user has checked all the boxes they want, I submit the form and call a javascript action where I need to get the various values of the checked checkboxes and fill in PayPal parameters and then some php code to fill in a database table with the purchased location/category. The AA, BB and CC indicate the name of a location in my database and the value 1, 2 and 3 indicates a id of a category record in the same database. These have to be sent to PayPal and then stored in the database.
... variou php code...
<input type=checkbox name=locA[] value=1 onclick=recalcTotal(this)>
<input type=checkbox name=locB[] value=1 onclick=recalcTotal(this)>
<input type=checkbox name=locC[] value=1 onclick=recalcTotal(this)>
<input type=checkbox name=locA[] value=2 onclick=recalcTotal(this)>
<input type=checkbox name=locB[] value=2 onclick=recalcTotal(this)>
<input type=checkbox name=locC[] value=2 onclick=recalcTotal(this)>
<input type=checkbox name=locA[] value=3 onclick=recalcTotal(this)>
<input type=checkbox name=locB[] value=3 onclick=recalcTotal(this)>
<input type=checkbox name=locC[] value=3 onclick=recalcTotal(this)>
...
<input type=checkbox name=locA[] value=n onclick=recalcTotal(this)>
<input type=checkbox name=locB[] value=n onclick=recalcTotal(this)>
<input type=checkbox name=locC[] value=n onclick=recalcTotal(this)>
...
more php code
 
javascript function recalctotal(obj) {
if obj.value=checked
   increment counter
else 
   decrement counter
}

Open in new window

0
Comment
Question by:Rich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24434183
First suggestion - don't worry about tracking the clicks when a user selects the checkboxes. That will require to continually run the recalctotal function with logic to add/remove from what ever variable you were using to store the state.

Instead - just run the function on form submit.

If you enclose all of your checkbox elements in say a div tag, with an ID, you can easily access all of the checkboxes. Then when the form gets submitted, you run a function to calculate the total or whatever ever it is you need to do. See the snippets of HTML and JavaScript below (untested and for example only)


<script type="text/javascript">
function recalctotal() {
      var checksDiv = document.getElementById("checks");
      var checkList = checksDiv .getElementsByTagName("input");
      for(var i = 0; i < checkList.length; i++)
      {
            if(checkList[i].checked) {
                  // add to tally or whatever you need to do
            }
      }
}
</script>

<form ... onsubmit="recalctotal();">
      <div id="checks">
            <input type=checkbox name=locA[] value=3 />
            <input type=checkbox name=locB[] value=3 />
            <input type=checkbox name=locC[] value=3 />
      </div>
</form>
0
 

Author Comment

by:Rich
ID: 24435409
Well, as far as the first part, I need to keep a form field called TotalCost updated as each button is checked or unchecked. I add or subtract from TotalCost.value based on the checked == true or false of the button being checked.

The second part should work for what I am doing, however, and I will try that.
0
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
ID: 24435511
OK, I think I understand why - I'm assuming it's to provide the user with live feedback of the totals.. Makes sense.

You can still use the script above, and call it on checkbox change.
0
 

Author Closing Comment

by:Rich
ID: 31583606
Thank you, that did the trick. So instead of passing only one value, I am passing the outer div element and waking through each checkbox.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question