Checkbox - Disable and change selected value

Disable and change a checkbox value in ChronoForms.

Overview

The issue occurs when trying to programmatically set a checkbox to a specific value and disable it based on conditions, but the enabling/disabling logic fails.
Use a CF variable to control the disabled state. Set the checkbox value directly in the data array and toggle the variable to true or false within your conditional PHP code, then reference that variable in the checkbox's disabled field.

Answered
ChronoForms v6
ms msreyes 21 Jul, 2020
I have a checkbox that 90% of the time user will just select either YES or NO.  Always enabled.

But when I retrieve a record under certain criteria I want to ensure the checkbox is selected with the NO option and DISABLED so user cannot change it.

I have the IF condition working, just don't know the syntax on how to get this to go to NO, and then disable the checkbox.
Max_admin Max_admin 22 Jul, 2020
Answer
1 Likes
use php:
$this->set("checkbox_disabled", false);
if(condition){
$this->data["checkbox_name"] = 0;
$this->set("checkbox_disabled", true);
}
then add {var:checkbox_disabled} in the disabled field in the checkbox settings!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- Elita- 31 Aug, 2021
Thanks Max, code is usable, however, I can get only checkbox disabled, bu cannot get it enabled. How that should be done? The code below not working.
obviously $this->set("checkbox_disabled", false); is not the right code😀

<?php

$klatiene= $this->data['klatiene'];
$klatiene_skaits=20;
$skaits=$this->data['skaits'];


// Izrekina atlikušo skaitu
$klatiene_atlikums=$klatiene_skaits-$klatiene;


// paziņojums
$this->set("checkbox_disabled", false);
if($klatiene_atlikums< $skaits) {
echo "Vietu skaits klātienē ir beidzies!";
$this->data["checkbox_name"] = 1;
$this->set("checkbox_disabled", true);
} else{
echo "<p>Atlikušais vietu skaits klātienē: " . $klatiene_atlikums . " vietas</p>";
$this->data["checkbox_name"] = 1;
$this->set("checkbox_disabled", false);
}

$this->data['klatiene_atlikums']=$klatiene_atlikums;
?>
sp spiceagent11 08 Sep, 2021
But when I retrieve a record under certain criteria I want to ensure the checkbox is selected with the NO option and DISABLED so user cannot change it.
This topic is locked and no more replies can be posted.