Forums

checkbox ghost value

skittle 26 Oct, 2023
Without the ghost value option on checkboxes in CF8, I am struggling with getting a value other than null from an unchecked checkbox to pass to a variable.
Any ideas on how to easily assign a 'ghost value' to unchecked checkboxes?

John
Max_admin 27 Oct, 2023
use this in a PHP action at the top of the "submit" area

if(!isset($this->data["field_name"])){
$this->data["field_name"] = "whatever";
}
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
skittle 27 Oct, 2023
Joomla4.x seems to handle checkboxes very differently than earlier versions. I have an older system running CF6/CC6 on Joomla3.10 which I am working on moving to Joomla4. I have the CF6 and CC6 for J4 installed on a J4 site and have restored some CF6 backups. Most things seem to work well but one of CF6 forms was not working like it did in J3.10. So, I decided to install CF8 and recreate the form from scratch. Once I had it setup, the CF8 form had the same problems that the CF6 form did.

As far as I can tell unchecked checkboxes are assigned a null value in J4 and this causes problems for some parts of the system. I had a simple JavaScript function that looked at the checkboxes 'checked' attribute as well as some other tasks. It always worked reliably in J3.10 but fails to run in J4 if the checkboxes are empty. The error basically says JS can't properly evaluate null values. This behavior is the same in CF6 for J4 and CF8 so it seems to be a J4 issue.

The checkbox null value seems to cause other issues. Max, in your response above, the code provided didn't really address the issue because the data variables were set when the page was submitted - so using !isset wasn't the best answer. I spent quite a bit of time trying to test with is_null in a PHP action which still doesn't help you to assign a value to a variable with null value. A variation of the PHP code worked fine for changing any value that was not null so I can verify that the PHP was correct and works in CF. In older versions of Joomla it was possible to use PHP to assign a non-null value to a variable that had null value.

What worked in the end was testing to see if the data had a value of '1' (the key I am using if the checkbox is checked). Here is the simple code called {var:strict_off} -

if($this->data["strict_onoff"] !== "1"){
$this->data["strict_onoff"] = "0";
}

My form needs to send the form values via a redirect when the Submit button is pressed. Previously, I would simply have the URL Parameters for the Redirect listed like this - strict_onoff={data:strict_onoff}. This won't work in J4 with data from empty checkboxes. The workaround is to enter the Redirect like this - strict_onoff={data:strict_onoff}{var:strict_off}. Having either of the {data:} or {var:} alone won't work - they need to be listed together as I have shown above.

As far as I can tell JavaScript functions that reference empty checkboxes will never run. I would love a solution to the JS issue.
Max_admin 28 Oct, 2023
this is how checkboxes work in general, when the checkbox is not checked then it sends nothing, so it does not even exist in the data array

v8 has a ghost value of empty string by default, so if the checkbox is not checked then it will be available in the data array but the value is empty string

JS can test the "checked" property of the checkbox field dom object
if(document.querySelector("#field_id").checked).....do something
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
skittle 28 Oct, 2023
Thanks Max. v8 having a ghost value of empty string makes sense. That's why my PHP function started working when I stopped testing to see if variable was null.

I'm still not sure how to avoid the JS error though. When my script looks at an unchecked checkbox
document.getElementById("strict_onoff").checked
all JS processing stops and I get an Uncaught TypeError: Cannot set properties of ..... null.
Max_admin 28 Oct, 2023
are you sure the checkbox id on the page is "strict_onoff" ? did you check the Elements in your browser's inspector ?
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
You need to login to be able to post a reply.