Intro:
I have a form, and PHP_SELF command
I have 3 checkboxes
<input type="checkbox" name="check1" value="25">
<input type="checkbox" name="check2" value="20">
<input type="checkbox" name="check3" value="15">
As you noticed VALUE field is different
And PHP code
GET for each of these
$value1='';
$value2= $check1 + $check2 + $check3;
The Problem:
I need the script to add values of any checked checkboxes on the page BUT also to present it on the same page (is it possible without loading the page again and loosing whats checked and what not)
Ex:
check1 (I checked it)
check2 (I checked it)
check3 (Not checked)
Output on the same page should be 45, but also the checkboxes that are checked should remain checked.
Thanks Ahead !
Math calculation of checkbox values on the same page
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Not with PHP it isn't you have to reload the page for the new data to get parsed. You can keep the the data in the checkboxes though by doing something like '
althought i recommend putting everything into an array and using a foreach statement to sort that all out.
and to get the value of all the checked boxes
To be honest I'm not sure if that's the correct way to do it but maybe it will give u an idea
Code: Select all
<?
if (!empty($value)) { echo "checked"; } // inside the checkbox
?>and to get the value of all the checked boxes
Code: Select all
<?php
foreach ($_POST["checkboxname"] as $checkbox)
{
if (!empty($checkbox))
{
$totalvalue += $checkbox;
}
}
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
<input type="checkbox" name="check3" value="15" <? if (!empty($value)) { echo "checked"; } ?>>If you don't mind having it reload once the form is submitted simply try that foreach statement at the top of your page.