Updating checkbox

Hello all and a Happy New Year to you,

I have searched vigorously through the forum but have not been able to find what might be wrong with my script. I have a form that records the checkbox to a table and I have created another form that will be used to update that information. I get the info from the table and display which checkbox has been checked but then after trying to change the checkbox it does not update.



        // link from previous page
        //  http://localhost/process_signatures_update_edit_gr.php?bid=579
        
        // javascript on this page - checkform(this)
/*            // activates the checkbox
            var inputs = document.getElementsByTagName('input');
            for(var c = 0; c < inputs.length; c++){
                if(inputs[c].type == "checkbox" && inputs[c].checked == true){
                //alert(inputs[c].value);
                array.push(inputs[c].value);
                }
            } 
*/

        // bid is a link as shown above - not a submit from the previous page
        settype($bid, 'integer');
        if(isset($_GET['bid'])) {
            $bid=$_GET['bid'];
        }

        $query = "select candidacy1, candidacy2, candidacy3, candidacy4 
            from $table_name WHERE bid = '$bid' ";
    
        $result = mysql_query($query) 
            or die ("Error in query: $query. <br />\
" . mysql_error() . "<br />\
" );
        $row = mysql_num_rows($result);
        while ($row = mysql_fetch_assoc($result)) {
            // set checkboxes from data
            $candidacy1 = $row["candidacy1"];
            $candidacy2 = $row["candidacy2"];
            $candidacy3 = $row["candidacy3"];
            $candidacy4 = $row["candidacy4"];
        }

echo '<form action="process_signatures_update_final_gr.php?bid=$bid" method="post" name="form" onsubmit="return checkform(this)" />'." \
";

        echo '<input type="hidden" id="candidacy1" name="candidacy1" value="0" />';
        echo '<input type="hidden" id="candidacy2" name="candidacy2" value="0" />';
        echo '<input type="hidden" id="candidacy3" name="candidacy3" value="0" />';
        echo '<input type="hidden" id="candidacy4" name="candidacy4" value="0" />';

// for brevity, I have placed only one of the four inputs here            
if ($candidacy1=='1')
            { echo "<input type=\\"checkbox\\" id=\\"candidacy1\\" name=\\"candidacy1\\" value=\\"$candidacy1\\" checked=\\"checked\\" /> \
"; }
            else
            { echo "<input type=\\"checkbox\\" id=\\"candidacy1\\" name=\\"candidacy1\\" value=\\"$candidacy1\\" /> \
"; }
            echo "If you check this box, your name will appear as &quot;Anonymos&quot;";

        echo '<input type="submit" id="mail" name="mail" class="button1" value="- update -" /> ';

if(isset($_POST['mail']))
{
            $candidacy1    = remove_headers($_POST['candidacy1']);

            $update = "update $table_name set 
                candidacy1     = '$candidacy1', 
            WHERE bid = '$bid' ";
            
            $result2 = mysql_query($update) or die(mysql_error());
            mysql_query("COMMIT");
}    // end isset submit

The expected result would be displayed on the next page.

The rest of the form updates so I guess that the problem has to be specifically with the checkbox.

Peter


$candidacy1    = remove_headers($_POST['candidacy1']);

$_POST[‘candidacy1’] will only exist if it was checked, so there could be an error or warning with this line when the checkbox is not checked.
How are you planning on storing the value in the DB? Often for boolean type flags (on/off situations) you’d use a TINYINT column and store 0 for no, 1 for yes.


$candidacy1 = (isset($_POST['candidacy1'])) ? 1 : 0;

$update = "update $table_name set 
            candidacy1     = $candidacy1
            WHERE bid = '$bid' ";

I’ve used the ternary operator, $candidacy1 will be 1 if checked, 0 otherwise.
Also, note there’s no comma in the SQL before the WHERE clause.

Hello cranial-bore,

Based on what you said, I just removed the input hidden to initialize the values before the inputs because they are being initialized from the db.
The values are being stored as 0 or 1 as tinyint.

I just noticed that I am receiving an error in my javascript.
Error: array is not defined

which is the js routine shown above.