Search code examples
phpjqueryconcrete5

Concrete5 form->checkboxes not working correctly


I'm new to concrete5 and PHP.

DB.XML

        <!-- features for Row 1 -->
        <field name="PC_Row_1_Feature_1_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>
        <field name="PC_Row_1_Feature_2_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>
        <field name="PC_Row_1_Feature_3_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>
        <field name="PC_Row_1_Feature_4_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>

        <!-- features for Row 2 -->
        <field name="PC_Row_2_Feature_1_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>
        <field name="PC_Row_2_Feature_2_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>
        <field name="PC_Row_2_Feature_3_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>
        <field name="PC_Row_2_Feature_4_Enabled" type="L">
            <default value="0" />
            <unsigned/>
        </field>

edit.php

<?php 
    echo $form->checkbox("PC_Row_1_Feature_1_Enabled", 1, $PC_Row_1_Feature_1_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_1_Feature_2_Enabled", 1, $PC_Row_1_Feature_2_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_1_Feature_3_Enabled", 1, $PC_Row_1_Feature_3_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_1_Feature_4_Enabled", 1, $PC_Row_1_Feature_4_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_2_Feature_1_Enabled", 1, $PC_Row_2_Feature_1_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_2_Feature_2_Enabled", 1, $PC_Row_2_Feature_2_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_2_Feature_3_Enabled", 1, $PC_Row_2_Feature_3_Enabled);
?>
<?php 
    echo $form->checkbox("PC_Row_2_Feature_4_Enabled", 1, $PC_Row_2_Feature_4_Enabled);
?>

I also tried with:

echo $form->checkbox('PC_Row_1_Feature_3_Enabled', $PC_Row_1_Feature_3_Enabled, false);

Also thought maybe a little JS would help

$('.checkbox input').on('click',function(){
    if($(this).val() == "0"){
        $(this).val('1');
        $(this).prop('checked', true);
    } else {
        $(this).val('0');
        $(this).prop('checked', false);
    }
});

To change the value and uncheck etc...

view.php

<?php if($PC_Row_2_Feature_1_Enabled == "1") { ?>
    <img class="ui centered image" src="<?php echo $this->getThemePath() ?>/images/tick_mark.png">
<?php } ?>

Issues I'm having is that when I check or uncheck it's not changing in database and then not showing or hiding on view. I know I'm likely to do be doing something wrong so hoping someone with concrete5 experience might be able to lend me a hand.


Solution

  • To save the checkbox value, you check to see if the key has been set in your block controller.php save() method. The save() method allows $_POST array data to be changed before saving to the database (e.g. if you wanted to trim() input).

    Example:

    public function save($args)
    {
        $args['checkbox_example'] = isset($args['checkbox_example']) ? 1 : 0;
        parent::save($args);
    }
    

    $args is the $_POST array.

    • if the checkbox_example key is set, the value is set to 1
    • if the checkbox_example key is not set, the value is set to 0