0

Let me present my code first:

<?php include ('header.php'); ?>
<h3>Extra menu items , van eigen pagina's</h3>
<!-- zet menu onderdelen aan of uit -->
<?php
    if ($_POST['submit'] == 'Verzenden') {
        $checkbox_vals = $_POST['np_menu_active_post']; // This will pass the selected checkbox values as an array

        //if(count($checkbox_vals) > 0) {
        // Loop it and update the values in DB
        foreach( $checkbox_vals as $key ){
            $updatequery = "UPDATE custompage set np_menu_active = '$key'";
            mysql_query($updatequery) or die("Couldn't get file list");
        }
    //}
?>
<meta HTTP-EQUIV="Refresh" CONTENT="0"; URL="<?php echo $_SERVER['PHP_SELF'];?>">
<?php
    }
?>

<?php
    $dbQuery_custom_toggle = "SELECT * ";

    $dbQuery_custom_toggle .= "FROM custompage";
    $result_custom_toggle = mysql_query($dbQuery_custom_toggle) or die("Couldn't get file list");
    while($row = mysql_fetch_array($result_custom_toggle)) {

        // $nptitel = $row['np_titel'];
        // $nptekst = $row['np_tekst'];

?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="hidden" name="np_menu_active_post[]" value="0" />
<?php echo $row['np_menu_titel'];?>&nbsp;<input type="checkbox" name="np_menu_active_post[]" value="1" <?php if($row['np_menu_active'] == "1"){echo 'checked="checked"';}?> />  <br />




<?php
    }
?>
    <input type="Submit" name="submit" value="Verzenden" class="btn-primary">
</form>
<!-- einde menu toggles -->
<br />
<br />

So im trying to make this form write the 1 value or 0 value based upon the checkboxed to the mysql db.

Its partly working, when i select the last checkbox, it checks all, and writes that to the db.

3
  • You forgot a WHERE condition in your UPDATE query. Mar 20, 2013 at 14:30
  • Hi rick, could you elaborate on this?
    – user1987641
    Mar 20, 2013 at 14:32
  • The checkbox vars will only be in the input data arrays if they are checked. Hence unchecked checkboxes will not have anything passed back for you to insert.
    – Kickstart
    Mar 20, 2013 at 14:38

1 Answer 1

0

With the foreach loop you update your db in the same field with each checkbox, so the last checkbox is the only thing that is left.

Depending on the desired result, you need to differ the column (so np_menu_active) for each checkbox, or need to add a WHERE condition (as Rick said) to differ the row.

E.g. If you would like to save the checkbox settings for multiple users, you would create a column for each checkbox and a row for each user.

2
  • hmm still lost, but ill try to figure it out, thnx
    – user1987641
    Mar 21, 2013 at 13:16
  • Im trying to use np_id to distinguish the checkboxes, however how do i pass a normal php variable into post? ( so not a form variable)
    – user1987641
    Mar 21, 2013 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.