how Joomla PHP: Checkbox won’t uncheck

 

Questions


Making a simple component. If I save a new record with the box checked, it saves fine. If I go back and uncheck a previously checked item it reverts back to checked. I thought it had something to do with a hidden field, played with putting one of those in manually but that didn’t fix it either so took it out.

Here’s where I’m at:
An xml snippet:

<fieldset name="checks">
  <field name="checkbox1"
    type="checkbox"
    label="First Checkbox"
    value="1"
    filter="intval"         
  /> 

   <field name="checkbox2"
    type="checkbox"
    label="Second Checkbox"
    value="1"
    filter="intval"         
  /> 

 ...

</fieldset>

The edit.php file:

<div class="width-45 fltlft">
  <fieldset class="adminform">
    <legend>Checkboxes</legend>
    <ul class="adminformlist">
      <?php foreach ($this->form->getFieldset('checks') as $field): ?>
        <li>
          <?php echo $field->label; ?>
          <?php echo $field->input; ?>
        </li>
      <?php endforeach ?>
    </ul>
  </fieldset>
</div>

Also added this towards the end before the form.token:

<input type="hidden" name="task" id="task" value="completion.edit" />

I tried deleting the value=”1″ in the xml but then I had the opposite problem where the check wouldn’t save at all.

Any ideas?

Thanks!

=============================

Edit:

Model:

<?php
defined( '_JEXEC' ) or die;

jimport('joomla.application.component.modeladmin');

class AssessModelCompletion extends JModelAdmin
{
//tells it what kind of record and the prefix

    public function getTable($type = 'Completion', $prefix = 'AssessTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    //Load the data into the edit form
    protected function loadFormData()
    {
        $data = JFactory::getApplication()->getUserState('com_assess.edit.completion.data', array()); //first try to get the data from the session, not db

        if (empty($data)) {
            $data = $this->getItem(); //this gets the data
        }

        return $data;

    }

    //Stores data in a session in case a field is missed
    public function getForm($data = array(), $loadData = true)
    {
        $form = $this->loadForm('com_assess.completion', 'completion', array('control' => 'jform', 'load_data' => $loadData));

        return $form;
    }
}

And table:

<?php 
defined ( '_JEXEC' ) or die;

class AssessTableCompletion extends JTable
{
    public function __construct(&$db)
    {
        parent::__construct('#__tablename_completions', 'completion_id', $db);
    }
}

======================================

References:
https://stackoverflow.com/questions/6964333/joomla-1-6-admin-form-processing-grouped-checkboxes-in-form

Tutorial code is not working any more in Joomla 2.5

http://docs.joomla.org/Developing_a_Model-View-Controller_%28MVC%29_Component_for_Joomla!1.7_-_Part_09#Adding_a_toolbar

 

 

————————————————-

Answer

joomla,joomla1.6,joomla1.7,joomla2.5,php

Facebook Comments

Post a comment