0

I have a while loop display checkboxes with a value assigned to each checkbox:

<input type='checkbox' name='rep[]' value='$invoiceID'>Reference Number: $invoiceID<input type='hidden' name='weektotal[]' value='$weekTotal'>";

For example if there were 3 checkboxes they'd look like this:

[] Reference Number: 1 (hidden value 30)
[] Reference Number: 2 (hidden value 50)
[] Reference Number: 3 (hidden value 40)

No if a user selected all 3 checkboxes how do I add all 3 hidden values together to insert that result in a field in the database (so the value would be 120). Or if the user selected the first 2 (so the value would be 80). I have tried this on the process page just before the INSERT statement:

$totalweek = array_sum($_POST['weektotal']);

And then use $totalweek in the VALUE for the insert.

This seemed to work but when I select the first one or the second checkbox on their own then it provides a result as if they were all being added up?

What is the correct way to do this?

1
  • Did you print_r($_POST['weektotal']) when it doesn't work as expect? Commented Dec 8, 2013 at 15:48

1 Answer 1

1

On the process page use following code:

$my_var = $_POST['rep'];
$var = $_POST['weektotal'];
$expense = $_POST['expensetotal'];
$total=0;
for($i=0;$i<sizeof($my_var);$i++){
$total=$total+$var[$i];
$exp = $exp+$expense[$i];
}
$grand_total = $total+$exp;
Sign up to request clarification or add additional context in comments.

5 Comments

This works great. If I have another hidden value called expensetotal would i just repeat this. I then would like to add the two together?
is the expensetotal associated with all checkbox or it is single hidden textbox
It would be assoicated with all checkboxes. There is an expensetotal for each checkbox.
Hello, just a quick question. This seems to be adding the main figures correctly but weirdly ignoring the decimals. I assume I need to put a number_format in there to show ,2 decimal places. Where would I put that?
^Ignore that comment, I wasn't passing the weektotal as 2 decimal places. My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.