Jump to content

changing the value of a checkbox in the database


Peggy

Recommended Posts

I have a form with check boxes in it. I didn't set the value of the check boxes I always thought the value was either 'on' or '' (empty). I have the value 'on' passing to the database. The problem is when I unchecked the box it isn't changing the value in the database to '' (empty). The rest of the values of the form, such as input boxes are being changed. Here is my code on my check boxes

 

<input  type="checkbox" name="form3_one_2" <?php if($form3_one_2 == 'on'){print 'checked="checked"';}?> />

 

Am I supposed to have an value put into the form??? :confused:

there's no need to touch your checkbox in the form.

 

If the value '' (empty) is not being recorded in your database, Then your php code is not sending it. Can we see your code the part you are handling the $_POST data and recording it to database table?

I don't think there is anything wrong with the code I'm using to post the info to the data base because the input boxes are being updated. Here is the code that updates the form anyway.

if(!empty($form_id)){

$sql = "UPDATE ".$form." SET ";

foreach($_POST as $key => $value){

if(($key != 'submit_'.$form ) && ($key != 'password2') && ($key != 'applicant_id') ){

$sql .=  $key. " = '$value',";

}

}

$sql = substr($sql, 0, -1);

$sql .= " WHERE ".$applicant_id." = $applicant_id";

$result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql");

}

I don't recommend this way of coding.

 

But you can modify it so that it gives you what you need:

I call the checkbox field "chkField"

 

 

<?php
if(!empty($form_id)){
      $sql = "UPDATE ".$form." SET ";
      foreach($_POST as $key => $value){
         if(($key != 'submit_'.$form ) && ($key != 'password2') && ($key != 'applicant_id') ){
            $sql .=  $key. " = '$value',";
         }
      }

      if (!isset($_POST['chkField']) || $_POST['chkField'] == '')
           $sql .=  "chkField = '',";

      $sql = substr($sql, 0, -1);
      $sql .= " WHERE ".$applicant_id." = $applicant_id";
      $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql");   
}
?>

the code:

if (!isset($_POST['chkField']) || $_POST['chkField'] == '')

          $sql .=  "chkField= '',";

 

works great only I have 200 checkboxes how can I incorporate this code into the loop:

foreach($_POST as $key => $value){} 

 

I have also found this code.

 

$one = isset($_POST['chkField']) ? 'on' : 'off';

 

this code works also but I haven't figured out how to put it into the loop

 

foreach($_POST as $key => $value){}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.