-2

I have a form of multiple checkboxes with each corresponding numeric values. I want the PHP to show the sum of the values from only the checked checkboxes. If checkbox 1 has a value of 80; checkbox 2 has 21; and checkbox three has 15; then as an example, if the user only checked checkboxes 3 and 2 then the PHP must output 36.

Here's what I have so far, concerning the HTML and CSS codes https://jsfiddle.net/nerdfighter/tmz1ymeL/4/ (jsFiddle) Any help would be appreciated! Thanks

<form action="index.php" method="get"/> 

PHP:

$val = 0;
if(isset($_POST['chck1']){
    $val += $_POST['chck1'];
}
if(isset($_POST['chck2']){
    $val += $_POST['chck2'];
}
if(isset($_POST['chck3']){
    $val += $_POST['chck3'];
}
echo $val;
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
ckdirecto
  • 41
  • 7
  • first, give the checkboxes a list name (so that PHP interprets it as array and not as single value). second, array_sum(). – Dormilich Mar 06 '18 at 16:58
  • 1
    I didn't downvote, but just FYI this was probably downvoted because you're asking for PHP help but haven't shown any attempt with calculating the value yourself. If you're not sure where to start, I'd recommend googling "php form submission" and figuring out how the `$_POST` (or `$_GET`) array works – WOUNDEDStevenJones Mar 06 '18 at 16:59
  • @WOUNDEDStevenJones I actually have started something but I can't seem to make it work hence this post – ckdirecto Mar 06 '18 at 17:00
  • @ckdirecto then please show us what you have so far. Random internet strangers are much more likely to help if it looks like if you've attempted this yourself first. Check out the links that Alon Eitan posted for assistance in writing a useful question. – WOUNDEDStevenJones Mar 06 '18 at 17:01
  • where is the php file? – Alessandro.Vegna Mar 06 '18 at 17:07
  • I'll try your edit and change the method to post. – ckdirecto Mar 06 '18 at 17:22
  • @WOUNDEDStevenJones can I use either GET/POST method without using servers? This is just a small school project and I probably will use client-side storage. – ckdirecto Mar 07 '18 at 06:02
  • It's kind of an odd question, but I guess so. The GET or POST will happen in the browser, but PHP is a server-side language. You could process GET data using JavaScript, and probably POST data too, but I'm not 100% sure. – WOUNDEDStevenJones Mar 07 '18 at 06:09
  • Ah, one option for you if you want to do everything client-side is ignore the actual form submission. Instead, you'll intercept the form submission (https://stackoverflow.com/questions/5384712/capture-a-form-submit-in-javascript) and then process the form data in JavaScript. – WOUNDEDStevenJones Mar 07 '18 at 15:31

2 Answers2

2

In your HTML change method="post" and fields name should be checkbox[]:

    <form action="index.php" method="post"/>
        <h1><a name = "VEGETABLES">VEGETABLES</a></h1><br>
        <h3>Carrots</h3>
        <label><input type="checkbox" name="checkbox[]" id="outside" class="sum" value="80" data-toggle="checkbox">Market Value Carrots</label><br>
        <label><input type="checkbox" name="checkbox[]" id="outside" class="sum" value="21" data-toggle="checkbox">Chantenay Carrots</label><br>
        <label><input type="checkbox" name="checkbox[]" id="outside" class="sum" value="15" data-toggle="checkbox">Loose Carrots</label><br>
        <input type="submit" value="SAD TRUTH"/>
    </form>

in your index.php:

    <?php
    if($_POST){
        $val = 0;
        foreach($_POST['checkbox'] as $checkbox){
            $val += $checkbox;
        }
        echo $val;
    }
    ?>
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
Nisha
  • 623
  • 5
  • 7
0

You're first going to have trouble because you're accessing the $_POST array for data, but your form method is method="get", so you should be using $_GET (or change your form method to method="post")

For checkboxes, you should change the name property as described at PHP Multiple Checkbox Array, so it'd be something like:

<h3>Carrots</h3>
<label><input type="checkbox" name="carrots[]" class="sum" value="81" data-toggle="checkbox">Market Value Carrots</label><br>
<label><input type="checkbox" name="carrots[]" class="sum" value="88" data-toggle="checkbox">Chantenay Carrots</label><br>
<label><input type="checkbox" name="carrots[]" class="sum" value="80" data-toggle="checkbox">Loose Carrots</label><br>

<h3>Cucumber</h3>
<label><input type="checkbox" name="cucumbers[]" class="sum" value="82" data-toggle="checkbox">Cucumber Portion</label><br>
<label><input type="checkbox" name="cucumbers[]" class="sum" value="80" data-toggle="checkbox">Cucumber Whole</label><br>

Then in PHP you can get the data like $carrots = $_POST['carrots'], so $carrots would be an array of all the checked values which you can loop through and sum the values.

(Additionally, you should remove/change the id="outside" for all inputs because IDs in HTML should be unique to the page - Does ID have to be unique in the whole page? for more info)

WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53