Paul-D Posted August 1 Share Posted August 1 Hi I have added a checkbox to a for [Bold] When submitted to the update php I get echo $Bold; On if it is ticked and Warning: Undefined array key "Bold" in /vhost/d/e/s/desmond-otoole.co.uk/www/bank2/StatementEntryUpdate.php on line 31 if it is not. Can someone help please. <code if($direction == "Update") { $SE_MyReason = $_POST['Reason']; // Keep the default regardless $SE_Money_In = $_POST['MoneyIn']; $SE_Money_Out = $_POST['MoneyOut']; $SE_DD_Entry = $_POST['DD_Entry']; $SE_MM_Entry = $_POST['MM_Entry']; $SE_YYYY_Entry = $_POST['YYYY_Entry']; $Comment = $_POST['Comment']; $Bold = $_POST['Bold']; <--- Line 31 echo $Bold; exit; </code> Quote Link to comment https://forums.phpfreaks.com/topic/322777-how-to-submit-a-checkbox/ Share on other sites More sharing options...
Barand Posted August 1 Share Posted August 1 Unchecked checkboxes are not posted. I prefer to use a the null coalescing operator (??) when handling checkboxes EG $Bold = $_POST['Bold'] ?? 0; //if not set, default to '0' 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/322777-how-to-submit-a-checkbox/#findComment-1631776 Share on other sites More sharing options...
Paul-D Posted August 1 Author Share Posted August 1 I used if set = 1 else 0. Posted returns 'On'' not a 1. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/322777-how-to-submit-a-checkbox/#findComment-1631779 Share on other sites More sharing options...
dodgeitorelse3 Posted August 1 Share Posted August 1 (edited) That's because if it is set it is using the value from your form input. Edited August 1 by dodgeitorelse3 Typo Quote Link to comment https://forums.phpfreaks.com/topic/322777-how-to-submit-a-checkbox/#findComment-1631794 Share on other sites More sharing options...
gizmola Posted August 1 Share Posted August 1 11 hours ago, Paul-D said: I used if set = 1 else 0. Posted returns 'On'' not a 1. Thanks. Yes, this is based on the value property of the checkbox. The important concept to remember is what Barand described: an unchecked checkbox will not exist in the $_POST. This is why Barand's code to utilize the null coalesce operator is generally speaking a best practice in PHP form handling where checkboxes are involved. Quote Link to comment https://forums.phpfreaks.com/topic/322777-how-to-submit-a-checkbox/#findComment-1631809 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.