So I have a form which is updating textfields fine but the checkbox values are not:

here is my textfield example:

<td><strong>What is it?</strong></td>
         <td width="39%"><textarea name="what_is_it" rows = "5" cols="40">' . $row['what_is_it'] . '</textarea></td>
        <td>&nbsp;</td>

here is the checkbox example

       <td><p> Independent, self-managing organisation, incorporated </p></td> 
       <td><input type="checkbox" name="independent" value="1"'; 
if ($row['independent'] == 1) {echo 'checked';} echo '>
</td> 

and here is a snapshot of the update code for each of these fields

<?php
$quality_name = $row['quality_name'];
if (isset($_POST['submit']) && $_POST['submit'] == 'update') 
{
//this is the update part
$sql = "UPDATE quality_info SET

what_is_it= '".$_POST['what_is_it']."',
independent= '".$_POST['independent']."'

WHERE quality_id = '".$row['quality_id']."' 


";
}

   $result = mysql_query($sql) or die(mysql_error()); 

?>

like i said the textfield updates fine but the checkbox results in a undefined index error

thx in advance

    The problem in general is that checkboxes to work properly is because they will all share a common name but a different value. Here is an example of how to set up a group of checkboxes in a form. e.g.

    <form method='post' action='process.php'>
    <input type = 'checkbox' name = 'vehicle[]' value = 'car' checked= 'checked'  />Car<br />
    <input type = 'checkbox' name = 'vehicle[]' value = 'pickup' />Pickup Truck<br />
    <input type = 'checkbox' name = 'vehicle[]' value = 'van' />Full Size Van<br />
    <input type = 'checkbox' name = 'vehicle[]' value = 'minivan' />Mini Van<br />
    <input type = 'checkbox' name = 'vehicle[]' value = 'suv' />SUV<br />
    <input type = 'submit' value ='Enter Data'>
    </form>

    Then create process.php as below and run the HTML

    <?php
    	echo "<pre>";
    	print_r($_POST['vehicle']);
    	echo "</pre>";
    ?>

    Test by checking none (you will have to uncheck the car), one, two and then all and see how the output works with checkboxes. Now you might see how to populate from the database a checkbox or group of checkboxes you could have a vehicle group, a meat group, a favorite drink group et cetera.

      Houdini wrote:

      they will all share a common name but a different value.

      Why is this? How come this is the case with checkboxes and not other input types. Makes no sense as what happens if you do not want to group the checkboxes?

        also - how would you go about posting ($_POST) an array value in terms of an update?

          Checkboxes are multidimensional because you have the ability to select zero, or any number of checkboxes so they all have a common variable name and can either be a numerically indexed array or they could be an associative array.
          <input type='checkbox' name='vehicle[car]' value='car'> is an associative array and to find its value you just use $_POST['vehicle']{car'] and if it were checked the value would be car and blank if not checked. Normally checkboxes concern multiple choices like name the sports you participate in with choices of

          • football

          • baseball

          • basketball

          • tennis

          • swimming

          • bowling

          • golf

          • hockey

          Naturally one could possibly be ver athletic and choose them all or vice versa so it would only make sense to have a group of checkboxes named something like sports[].

            OK so if I had say a group of checkboxes based on a question what number of paid employees does this relate to: my html would read:

            <input type='checkbox' name='paid_employees[none]' value='none'> is an associative array and to find its value you just use $_POST['vehicle']{car']

            <input type="checkbox" name='paid_employees[none]' value="n
            one">
            
               <input type="checkbox" name='paid_employees[5less]' value="5 or less">
            
              <input type="checkbox" name= 'paid_employees[6to20]'value="6 to 20">
            
            <input type="checkbox" name= 'paid_employees[over20]' value="over 20">
            
            
            

            but how would I echo these values i.e (my old approach):

            <input type="checkbox" name="independent" value="1"'; 
            if ($row['independent'] == 1) {echo 'checked';} echo '>
            
              <td><input type="checkbox" name="paid_employees[none]" value="0">None</td>
              </tr>
              <tr>
              <td><input type="checkbox" name="paid_employees[5]" value="5">5 or less </td>
              </tr>
              <tr>
              <td><input type="checkbox" name="paid_employees[6]" value="6">6 to 20</td>
              </tr>
              <tr>
              <td><input type="checkbox" name="paid_employees[20]" value="20">Over 20</td>

              Now you have an array $paid_employees and depending on which was checked would give a different value, but in this case although they do have something in common it is nonsensicle to have the ability to select both none and over twenty, so in the above you should instead use a radio button which kicd of like checkboxes share a common name but have different values and for this reason only one radio button may be selected.

                I would like to choose more than one checkbox in the array- in fact in this case I would like to choose between 0 and 4 checkboxes. The question in this instance is how many number of employees does the quality standard suit?- so this could be any of the options listed. Does this mean a radio button (not grouped) would be more appropriate and if so do I stick with the same approach before for updating echoing?

                  OK if you would like the ability to select any amout of the values then yes you would want to use a checkbox. Then the array as I showed above would be accessed by using $POST['paid_employees'][0] if checked it would equal 0 otherwise it would be blank. What I do with such $POST data after submission is assign short variables to them using the following.

                  $aneurysm =($_POST['health'][aneurysm]== 'aneurysm'? '1': '0');
                  $angina =($_POST['health'][angina]== 'angina'? '1': '0');
                  $anorexia =($_POST['health'][anorexia]== 'anorexia'? '1': '0');
                  $anxiety =($_POST['health'][anxiety]== 'anxiety'? '1': '0');
                  $apnea =($_POST['health'][apnea]== 'apnea'? '1': '0');
                  $appendicitis =($_POST['health'][appendicitis]== 'appendicitis'? '1': '0');

                  The above are but six of 348 checkboxes for a medical form where patients might have many of these now or in the past or possibly a family member with such conditions. They are put into a database with a value of 1 or 0 depending on if they were checked or not. Then if you pull the data of a patient id that has filled out the form you can see what all medical complaints they might have now or in the past. What is being used is a ternary operator and if the box is checked and the value is what I check for it set that value to 1 otherwise it sets the variable to 0. It is a kind of shorthand way of using if else statements.

                    cool - so am I right in saying that when you echo the checkbox value it would be like this

                            <td><p> Independent, self-managing organisation, incorporated </p></td> 
                           <td><input type="checkbox" name="quality[independent]" value="1"'; 
                    if ($row['independent'] == 1) {echo 'checked';} echo '>
                    </td> 
                    

                    and when you actually do the update it would go like this

                    $independent =($_POST['quality'][independent]== 'independent'? '1': '0'); 
                    $coltdbyguarantee =($_POST['quality'][coltdbyguarantee]== 'coltdbyguarantee'? '1': '0'); 
                    //etc etc
                    
                    if (isset($_POST['submit']) && $_POST['submit'] == 'update') 
                    {
                    //this is the update part
                    $sql = "UPDATE quality_info SET
                    independent= '$independent',
                    coltdbyguarantee= '$coltdbyguarantee'
                    //etc et
                    
                    
                    WHERE quality_id = '".$row['quality_id']."' 
                    
                    
                    ";
                    }
                    
                       $result = mysql_query($sql) or die(mysql_error()); 
                    
                    ?>
                    
                      Write a Reply...