Search code examples
phphtmlformscheckboxisset

Checkbox increasing value in database


Yes this is one of those checkbox Form questions. Im trying to keep my question short and simple to understand. I have a form with about 25 checkboxes, divided into 3 groups. Each group gives a different point for each checked box. Group1 = 5points/check | Group2 = 8points/check | Group3 = 12points/check. What I want to do is to count number of checks on submit and then add everything to a total score. Simple? New boxes can be checked at any time, which means that the total score can increase, and to make it easier, checkboxes can't be unchecked so the total score can't decrease. The total score will then be saved in a database table. This is what i've got right now, it's not much, but I need new eyes on my problem. The equation seems easy in my head, but I can't get it on paper.

            ....5points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="five[]" value="1" <?php if(isset($_POST['five'])) echo 'checked="checked"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="five[]" value="1" <?php if(isset($_POST['five'])) echo 'checked="checked"'; ?> />

                    <label for="first_name">n point</label>
                    <input type="checkbox" name="five[]" value="1" <?php if(isset($_POST['five'])) echo 'checked="checked"'; ?> />
................8points
                    <label for="first_name">first point</label>
                    <input type="checkbox" name="eight[]" value="1" <?php if(isset($_POST['eight'])) echo 'checked="checked"'; ?> />

                    <label for="first_name">sec point</label>
                    <input type="checkbox" name="eight[]" value="1" <?php if(isset($_POST['eight'])) echo 'checked="checked"'; ?> />
....12points and so on

also Im saving the boxes as checked with the Isset = checked code. This is my ugly addition of total score:

$a =0;
$b =0;
$total;
if(isset($_POST['submitted']))
{
if (isset($_POST['five'])) {
    foreach ($_POST['five'] as $five) {
       $a + 5;
    }
 if (isset($_POST['eight'])) {
    foreach ($_POST['eight'] as $eight) {
       $b + 8;
    }
    $total=$total+$a+$b;
}

But of course it does not do the trick, it just gives me an echo result as 5555555 and 888888. Also I don't want my total to add the same score twice which it seems to do now. Can someone help me straight this brain teaser out?


Solution

  • change $a + 5 and $a + 8 to:

    $a += 5;
    $a += 8;
    

    Or you can make the whole counting even smarter, like this:

    if(isset($_POST['submitted'])) {
      $fives = isset($_POST['five']) ? $_POST['five'] : array();
      $eights = isset($_POST['eight']) ? $_POST['eight'] : array();
      $twelves = isset($_POST['twelve']) ? $_POST['twelve'] : array();
    
      $total = 5 * count($fives) + 8 * count($eights) + 12 * count(twelves);
    }
    

    Also, your html propbly checks all the checkboxes if submitted. Change the if statements, names, and add disabled property if you don't want points to be substracted:

                ....5points
                        <label for="first_name">first point</label>
                        <input type="checkbox" name="five[0]" value="1" <?php if(isset($_POST['five'][0])) echo 'checked="checked" disabled="true"'; ?> />
    
                        <label for="first_name">sec point</label>
                        <input type="checkbox" name="five[1]" value="1" <?php if(isset($_POST['five'][1])) echo 'checked="checked" disabled="true"'; ?> />
    
                        <label for="first_name">n point</label>
                        <input type="checkbox" name="five[2]" value="1" <?php if(isset($_POST['five'][2])) echo 'checked="checked" disabled="true"'; ?> />
    ................8points
                        <label for="first_name">first point</label>
                        <input type="checkbox" name="eight[0]" value="1" <?php if(isset($_POST['eight'][0])) echo 'checked="checked" disabled="true"'; ?> />
    
                        <label for="first_name">sec point</label>
                        <input type="checkbox" name="eight[1]" value="1" <?php if(isset($_POST['eight'][1])) echo 'checked="checked" disabled="true"'; ?> />
    ....12points and so on