1

I'm having trouble in getting the values of the checked and unchecked checkbox. When the checkbox is checked, it will get the value of that particular checkbox and whenever the checkbox is unchecked, the value of the unchecked checkbox will be 0.

This is my html code:

<table border="0" cellspacing="0" cellpadding="1">
        <tbody>
        <tr style="height: 40px;">
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="5"> Barangay business permit </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="10"> Business plate </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="20"> Sanitary inspection fee </td>
        </tr>
        <tr style="height: 40px;">
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="30"> Environmental clearance </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="40"> Barangay development </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="50"> building permit </td>
        </tr>
        <tr style="height: 40px;">
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="60"> Electrical inspection </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="70"> Mechanical inspection    </td>
            <td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="80"> Plumbing inspection </td>
        </tr>
        </tbody>
</table>
<div class="" style="margin: auto; padding-top:20px;">Total <input type="text" name="total" style="color: #222222;" readonly/>
<input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;">
</form>

PHP code:

$checkbox=$_POST['checkbox'];

    for($i=0; $i<count($checkbox); $i++)
    {
        $num[$i]=$checkbox[$i];
    }

Javascript codes (for the function of getting the values of each checkbox while checking and generating a total in the total field.)

<script type="text/javascript">
    $(function(){

        var applications = [];
        //bind the change event to the checkboxes
        $('input[name="checkbox[]"]').change(function(){
            var total = 0;
            //get value from each selected ckeck box
            $('input[name="checkbox[]"]:checked').each(function(){
                var tval = $(this).val();
                total += parseFloat(tval);
            });
            //finally display the total with a ₱ sign
            $('input[name="total"]').val("₱ " + total);

            // handle applications
            var application = $.trim($(this)[0].nextSibling.nodeValue); // get the name
            var i = applications.indexOf(application);
            if(i != -1) {
                applications.splice(i, 1); // remove if unchecked
            } else {
                applications.push(application); // push if checked
            }
            var temp = applications.join(', ');
            $('input[name="applications"]').val(temp);

        });

    });
    </script>

In the actual test, I checked some of the checkbox in the table [0, 1, 0, 0, 1, 1, 0, 0, 0]. Note: 1 represents as checked and 0 represents as unchecked to make it clear.

When I click on submit button and display the values, I got this

[0] => 10

[1] => 40

[2] => 50

But I want to get the values like this

[0] => 0

[1] => 10

[2] => 0

[3] => 0

[4] => 40

[5] => 50

[6] => 0

[7] => 0

[8] => 0

How can I get that values? I need to get each value of the unchecked as 0 & checked values then save each array (in order) to database. I really need a help, I searched it from the net but I didn't get the right solution to my problem. I think my php codes is wrong.

database name: application

table: fees

columns: ID, fee1, fee2, fee3, fee4, fee5, fee6, fee7, fee8, fee9

Kevin
  • 41,694
  • 12
  • 53
  • 70
Ejardy
  • 33
  • 2
  • 10

3 Answers3

1

By the looks of it, you can use array_replace on this.

First build the default structure thru array_fill() (since unchecked checkboxes are not going to be included the POST). Then finally array_replace the default with the input. Example:

Demo

<?php

$default = array_fill(0, 9, 0);
if(isset($_POST['register'])) {
    $input = $_POST['checkbox'];
    $final = array_replace($default, $input);
    echo '<pre>';
    print_r($final);
}

?>
<form method="POST">
    <table border="0" cellspacing="0" cellpadding="1">
            <tbody>
            <tr style="height: 40px;">
                <td style="width: 240px;"><input type="checkbox" name="checkbox[0]" value="5" class="toggle_checkbox" /> Barangay business permit </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[1]" value="10" class="toggle_checkbox" /> Business plate </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[2]" value="20" class="toggle_checkbox" /> Sanitary inspection fee </td>
            </tr>
            <tr style="height: 40px;">
                <td style="width: 240px;"><input type="checkbox" name="checkbox[3]" value="30" class="toggle_checkbox" /> Environmental clearance </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[4]" value="40" class="toggle_checkbox" /> Barangay development </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[5]" value="50" class="toggle_checkbox" /> building permit </td>
            </tr>
            <tr style="height: 40px;">
                <td style="width: 240px;"><input type="checkbox" name="checkbox[6]" value="60" class="toggle_checkbox" /> Electrical inspection </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[7]" value="70" class="toggle_checkbox" /> Mechanical inspection    </td>
                <td style="width: 240px;"><input type="checkbox" name="checkbox[8]" value="80" class="toggle_checkbox" /> Plumbing inspection </td>
            </tr>
            </tbody>
    <input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;">
    </table>
</form>

Sidenote: You need to explicitly put indices on the name attribute so that they are going to be aligned when the replace takes in.

And then in your JS:

Since you cannot use the name calling because of the indices, turn it into classes instead:

$('.toggle_checkbox').change(function(){
    var total = 0;
    //get value from each selected ckeck box
    $('.toggle_checkbox:checked').each(function(){
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • I edited my question and added the javascript function. I tried your codes but the automatic generation of the total values while checking the checkbox is not working because I think changing the checkbox[] name to checkbox[with numbers] will affect the javascript function. – Ejardy Sep 23 '14 at 04:13
  • @Ejardy just change the `.change` to `$('.toggle_checkbox')` instead of pointing the `name=""` – Kevin Sep 23 '14 at 04:15
  • @Ejardy i made the revision try again – Kevin Sep 23 '14 at 04:17
  • sorry for late reply. Your code works! But how can I assign per array in a variable for me to save it to database? – Ejardy Sep 23 '14 at 04:51
  • @Ejardy normally just loop it, under the loop is the insertion query, or you can also build a query such that it will inert multiple rows. – Kevin Sep 23 '14 at 05:26
  • sa barangay tol! project eh hehe! sorry late reply, nasa work kasi. – Ejardy Sep 23 '14 at 08:09
  • @Ejardy ahh okay bro no prob – Kevin Sep 23 '14 at 08:10
  • bro are you familiar with the codes of one time download only? Thanks! – Ejardy Sep 24 '14 at 02:20
  • @Ejardy too broad bro, can be done in myriads of ways, maybe add a flag in DB table `is_downloaded`, initial value `0`, after the first download, update to `1`, so that it can never be downloaded again – Kevin Sep 24 '14 at 02:27
  • but what if the download is cancelled by the user or internet disruptions occurred before the download finishes? – Ejardy Sep 24 '14 at 02:32
  • @Ejardy yeah i thought so too, maybe this could shed some light, http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – Kevin Sep 24 '14 at 02:34
  • Maybe I'll go first to the easiest way for deleting the file from the database. I can't fully understand how the codes are working. Thanks for the suggestions! I will just change my download php soon when I learned that codes. – Ejardy Sep 24 '14 at 03:44
  • I have a problem with the radio buttons. Here is the link [RadioButtons](http://stackoverflow.com/questions/26603155/checking-the-correct-answer-from-the-database-by-a-checked-radio-button-in-php/26603425?noredirect=1#comment41838514_26603425). I hope you can help me tol :) – Ejardy Oct 29 '14 at 02:28
  • @Ejardy imo, you could just follow Shaunak's answer, create a table for the answers as well, and use the relationships between that table questions and table answers, `question_id` etc. then during the form, if this is a set of questions (`5 or 10, or whatever`). load each set of question and then save each user selection in a session – Kevin Oct 29 '14 at 02:32
  • My answers is located inside the table of my question. I don't know exactly what it looks like the code for comparison of the selected radio button to the correct answer in the database. I mean the codes inside the $_POST['finish']; which is the button. – Ejardy Oct 29 '14 at 03:39
  • @Ejardy then just catch the form submission, get all user input. (including which questions have been presented, since its randomized). then compare the selected answer against the correct answer – Kevin Oct 29 '14 at 03:53
  • I really don't have any idea how to get the values. I'm just a newbie in php and I have to learn a lot of things about php. kain muna ako. – Ejardy Oct 29 '14 at 04:09
  • @Ejardy just access post values like you normally would do `$_POST['radio']` blah blah something like that. most likely this will return an array. the keys of that array are the ids since you explicitly set them inside the markup. `name[radio][]`, etc. – Kevin Oct 29 '14 at 04:13
1
<input type='hidden' name='checkbox[0]' value='0'/>
<input type='checkbox' name='checkbox[0]' value='10'/>

Because the names are the same, php will overwrite the default value with the checkbox value if it is present.

Mike
  • 2,721
  • 1
  • 15
  • 20
0
$("#save").click(function(){
var check = [];
$('.checkBox:checked').each(function() 
{check.push($(this).val());});
var count=check.length;
var data={"Header":"some data","details":[]};
var detail={"values":""};
for(i=0;i<=count-1;i++){
var val=check[i];
detail.values=val;
data.details.push(detail);
detail={"values":""};
}
$.ajax({
url:"data/save",
data:JSON.stringify(data),
success:function(){
alert("sukses !!");
}
});

});
surya handoko
  • 354
  • 3
  • 9