954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

checkbox values calculation with PHP

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?

egoleo
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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;
}
?>
techniner
Posting Pro
527 posts since May 2005
Reputation Points: 12
Solved Threads: 19
 

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.

egoleo
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

techniner
Posting Pro
527 posts since May 2005
Reputation Points: 12
Solved Threads: 19
 

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 ";
            }
            $content = '<hr>';
            $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>
            <input type="checkbox" name="check_list[]" value="1">ASP
            <input type="checkbox" name="check_list[]" value="2">PHP
            <input type="checkbox" name="check_list[]" value="3">JavaScript
            <input type="checkbox" name="check_list[]" value="4">HTML
            <input type="checkbox" name="check_list[]" value="5">MySQL

            <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. :)

joshisumitnet
Light Poster
26 posts since Jan 2009
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You