The following warnings occurred:
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.31 (Linux)
File Line Function
/global.php 783 errorHandler->error
/printthread.php 16 require_once
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.31 (Linux)
File Line Function
/global.php 783 errorHandler->error
/printthread.php 16 require_once
Warning [2] Undefined variable $newpmmsg - Line: 40 - File: global.php(841) : eval()'d code PHP 8.1.31 (Linux)
File Line Function
/global.php(841) : eval()'d code 40 errorHandler->error
/global.php 841 eval
/printthread.php 16 require_once
Warning [2] Undefined array key "style" - Line: 909 - File: global.php PHP 8.1.31 (Linux)
File Line Function
/global.php 909 errorHandler->error
/printthread.php 16 require_once
Warning [2] Undefined property: MyLanguage::$lang_select_default - Line: 5024 - File: inc/functions.php PHP 8.1.31 (Linux)
File Line Function
/inc/functions.php 5024 errorHandler->error
/global.php 909 build_theme_select
/printthread.php 16 require_once
Warning [2] Undefined array key "additionalgroups" - Line: 7162 - File: inc/functions.php PHP 8.1.31 (Linux)
File Line Function
/inc/functions.php 7162 errorHandler->error
/inc/functions.php 5044 is_member
/global.php 909 build_theme_select
/printthread.php 16 require_once
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.31 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1370 fetch_forum_permissions
/printthread.php 76 forum_permissions
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.31 (Linux)
File Line Function
/printthread.php 165 errorHandler->error



Form Tools
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

$fields 
ft_api_init_form_page();
// ... 

if (isset($_POST["your_submit_btn_name"])) {
  
$_POST["cb_name"] = (isset($_POST["cb_name"])) ? isset($_POST["cb_name"]) : "";
}

// ... 
$params = array(
  
// ...
);
?>

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.

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

$fields 
ft_api_init_form_page();
// ... 

if (isset($_POST["your_submit_btn_name"])) {
  
$_POST["cb_name"] = (isset($_POST["cb_name"])) ? isset($_POST["cb_name"]) : "";
}

// ... 
$params = array(
  
// ...
);
?>

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

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:
// ...

if (isset($_POST['submit_button_name']))
{
  
$_POST["cb_name"] = (isset($_POST["cb_name"])) ? isset($_POST["cb_name"]) : "None";

// ...

<input name="cb_name[]" type="checkbox" value="Value if selected." <?php if (@$fields["cb_name"] == "Value if selected.") echo "checked"?> /> 



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."