I have checkboxes with an amount as values.
Can someone help on how to fine the total of
checkbox values and sent to an email using php?

Recommended Answers

All 4 Replies

Put the check boxes in a form

<form method="post">
<input TYPE="checkbox" NAME="five" VALUE="5" checked>Five
<input TYPE="checkbox" NAME="ten" VALUE="10" checked>Ten
<input type="submit" name="go" value"Submit This">
<form>

Then Above that you put:

<?

if ($go) {

$add_this=$_POST['five'];
$add_that=$_POST['ten'];
$total=$add_this+$add_that;

echo $total;
}
?>

Thanks. but It did not work. When i click on it nothing happens.
maybe i should show u the site which i am using it on so that u can help me on it.

Yes. Paste me your code and I will do it for you.

Hi, Friends.
Here is the easiest way to select multiple checkboxes and then also reading their values and make our task.

The below code is to read the checkbox array. Its self explanatory.

<?php
if(isset($_POST['submit'])) {
    if(isset($_POST['check_list'])) {
        $fields = $_POST['check_list'];
        if (is_array($fields)) {
            echo "<pre>";
            print_r($fields);
            echo "</pre>";
            foreach ($fields as $key=>$val) {
                echo "$key -> $val <br />";
            }
            $content = '<hr><br>';
            $content = $content . count($fields);
            for ($i=0;$i<count($fields);$i++) {
                $content = $content . "<li>$fields[$i]\n";
            }
            echo $content;
        }
    }
}
?>

In the below HTML and JAVASCRIPT code, nice work has been done. In JavaScript first we are finding the "check_list" array and then assigning it to out array variable and then selecting multiple checkboxes.

<html>
    <head>
        <title>CheckAll and UnCheckAll</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script LANGUAGE="JavaScript">
            function CheckAll()
            {
                var a=new Array();
                a=document.getElementsByName("check_list[]");
                //alert("Length:"+a.length);
                /*var p=0;
                for(i=0;i<a.length;i++){
                    if(a[i].checked){
                        alert(a[i].value);
                        p=1;
                    }
                }*/
                for (i = 0; i < a.length; i++)
                    a[i].checked = true ;
            }

            function UnCheckAll(chk)
            {
                var a=new Array();
                chk=document.getElementsByName("check_list[]");
                for (i = 0; i < chk.length; i++)
                    chk[i].checked = false ;
            }
        </script>
    </head>
    <body>
        <form name="myform" action="checkbox1.php" method="post">
            <b>Scripts for Web design and programming</b><br>
            <input type="checkbox" name="check_list[]" value="1">ASP<br>
            <input type="checkbox" name="check_list[]" value="2">PHP<br>
            <input type="checkbox" name="check_list[]" value="3">JavaScript<br>
            <input type="checkbox" name="check_list[]" value="4">HTML<br>
            <input type="checkbox" name="check_list[]" value="5">MySQL<br>

            <input type="button" name="Check_All" value="Check All" onClick="CheckAll();">
            <input type="button" name="Un_CheckAll" value="Uncheck All" onClick="UnCheckAll();">
            <input type="submit" name="submit" value="submit">
        </form>
    </body>
</html>

Try it. It works nice.
Cheer. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.