HarryMW Posted June 28, 2011 Share Posted June 28, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/240610-checkbox-foreach-01/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 28, 2011 Share Posted June 28, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/240610-checkbox-foreach-01/#findComment-1235847 Share on other sites More sharing options...
HarryMW Posted June 28, 2011 Author Share Posted June 28, 2011 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/240610-checkbox-foreach-01/#findComment-1235910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.