The following warnings occurred: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.31 (Linux)
|
![]() |
Checkbox: Once checked forever checked? - Printable Version +- Form Tools (https://forums.formtools.org) +-- Forum: Form Tools (https://forums.formtools.org/forumdisplay.php?fid=1) +--- Forum: General Discussion (https://forums.formtools.org/forumdisplay.php?fid=5) +--- Thread: Checkbox: Once checked forever checked? (/showthread.php?tid=1787) |
Checkbox: Once checked forever checked? - Xenophon - Nov 29th, 2011 Hello Ben/Seniors Hope everyone is well! I have a checkbox value doesnt get updated when user goes back to change. If the checkbox gets checked once, the resulted value forever remains as checked regardless what update follows thereafter. Is this the very nature of a checkbox or something i did wrong? Scenarios OK: FORM page (by default: unchecked) --> REVIEW page (unchecked) FORM page (checked) --> REVIEW page (checked) Scenarios NOT OK: FORM (checked) --> REVIEW (checked) --> goes back to FORM (update: unchecked) --> REVIEW (still checked) My code: <input type="checkbox" id="only genius knows php" name="only genius knows php" value="Yes" onclick="disable_dropdown(this.checked)" <?php if (@$fields["only genius knows php"] == "Yes") echo "checked"; ?> /> Big thanks in advance to any help! RE: Checkbox: Once checked forever checked? - Ben - Dec 4th, 2011 Ahh! Yeah, this just tripped me up myself. So the funny thing about checkboxes is that if nothing's checked, nothing will be sent along in the POST request and that particular field won't be updated in the database. Sadly, this is just the way HTTP works for form submissions. So to get around it with API forms, you need to add in a line of code to say if the form has been submitted and there's nothing for the checkbox field, set it to blank. Otherwise, store whatever value is being passed. Here's some example code: PHP Code: <?php You'll need to change "your_submit_btn_name" to, well, your submit button name, and "cb_name" to whatever name attribute your checkbox fields have. I put the other code in just to give you an idea of where it should go. Hope this helps! - Ben RE: Checkbox: Once checked forever checked? - Xenophon - Dec 5th, 2011 (Dec 4th, 2011, 2:08 PM)Ben Wrote: Ahh! Yeah, this just tripped me up myself. Awesome! Fabulous! Gorgeous! Brilliant! That works beautifully and it has now solved my problem. Thanks Ben, Genius! RE: Checkbox: Once checked forever checked? - Ben - Dec 6th, 2011 Haha :-) No worries! RE: Checkbox: Once checked forever checked? - PTFool - May 24th, 2012 I am having the same issue with an array of checkboxes. What would be the code for this? Thanks, Randy RE: Checkbox: Once checked forever checked? - michatmaster7 - May 31st, 2012 Oh my. I'm surprised I don't remember reading this thread before. Ben - please tell me that this was addressed in a core update, where you made this code standard? I haven't run into this, but I haven't been looking for it, either. RE: Checkbox: Once checked forever checked? - PTFool - May 31st, 2012 I did a work around on this by adding another element to the array that is hidden in the form. RE: Checkbox: Once checked forever checked? - michatmaster7 - May 31st, 2012 Could you be more specific? RE: Checkbox: Once checked forever checked? - michatmaster7 - Sep 12th, 2013 I admit I'm just getting around to this. I thought it would be a great addition to all my forms, because rather than have the form send a blank value, I could tell the form "if the checkbox is left unchecked, the send {some value}, otherwise send it's checked value." Well. It half worked. Now when the form is submitted, if the checkbox is left unchecked, it records the value from Ben's code above. However, if it IS checked, it always records a value of "1" - regardless of what the actual value is. This, of course, is not ideal. I have most checkboxes set as text fields in the FT backend, but that shouldn't matter. So... is there a problem with the code above? Why is submitting a "1" when the box IS checked? Example Code: PHP Code: // ... RE: Checkbox: Once checked forever checked? - mikkolindberg - Feb 7th, 2017 Figured I would post the solution to the issue with "1" being echoed. Slight change: if (isset($_POST["your_submit_btn_name"])) { $_POST["cb_name"] = (isset($_POST["cb_name"])) ? "WhateverYouWantToEcho" : ""; } The check logic goes, "when the user hits the submit button, test if the field (checkbox) is checked, if yes, echo this, if not, echo that." |