Jump to content

Checkbox Foreach 0/1


HarryMW

Recommended Posts

Hello,

 

In short, i'm generating rows of records, and checkboxes with them that represent either 1 for ticked or 0 for not...

 

The echo tickbox:

<?php echo '<input name="abc_'.$row_ab['id'].'" value="1" type="checkbox" id="val" '; if($row_ab['ABC']>=1) echo 'checked="checked"'; echo ' />';?>

 

The top code:

<?php   

   foreach ($_POST as $fieldname => $field) {

       if((substr($fieldname,0,4) == "abc_") == '1'){
   $ABC = '1';
} else {
   $ABC = '0';
}

           ...

	if (substr($fieldname,0,4)=="abc_")
	    mysql_query("UPDATE `table` SET `ABC` = '$ABC' WHERE `id`='".substr($fieldname,4)."' AND `ID` = '$ID'");

}
?>

 

Now the problem is, obviously it will always set the value to 1; I dont quite know what to replace the substr with as it will always be bifferent depending on the id of the entry i'm not too sure how I change rectify that, any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/240610-checkbox-foreach-01/
Share on other sites

Checkboxes that are not checked won't be submitted by the browser and won't exist in the $_POST array.

 

What you need to do is get/have an array of the checkbox field names (the same way that you produced the form fields) and use a foreach loop on this array. You would then check if each expected field name isset or not and use that to give your your 0 or 1 value to be use in the query(ies.)

Link to comment
https://forums.phpfreaks.com/topic/240610-checkbox-foreach-01/#findComment-1235847
Share on other sites

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.