Jump to content

Add multiple checkbox to contact form


nbarnum

Recommended Posts

I want to add a checkbox to the contact form.  They user could select more than one box.  I'm guessing I need some sort of foreach loop but I can only get it to post the last checked item and not the others.

 

In the front controller I have this in the postProcess function.

 

foreach(Tools::getValue('gcASGroup') as $gcASGroup);

 

I figure I'm missing something somewhere.

Link to comment
Share on other sites

I once created some module where I wanted to select one or more features on a screen to do something with it.

 

I used this code. Maybe not optimized, but for tutorial's sake maybe useful.

 

 

public function getContent()
{
  $output = null;
 
  if (Tools::isSubmit('submit'.$this->name)) {
    // Get default language
    $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
 
    // Store features to sort on
    $arrayIndex = 1;
    foreach (Feature::getFeatures($default_lang) as $feature)
      $featuresToStore[$arrayIndex] = (Tools::getValue('selectedFeatures_'.$arrayIndex++) != '' ? 
          $feature['id_feature'] : '');
 
    if (!$featuresToStore || empty($featuresToStore))
      $output .= $this->displayError($this->l('Invalid feature ID value. Check if features are defined'));
    else {
      Configuration::updateValue('PS_SORTGROUPSORT_FEATURES_SELECTED', implode('|', $featuresToStore));
      $output .= $this->displayConfirmation($this->l('Settings updated'));
    }
  }
  return $output.$this->displayForm();
}
 
 
public function displayForm()
{
  // Get default language
  $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
 
  $fields_form[0]['form'] = array(
      'legend' => array(
          'title' => $this->l('Choose features to sort on'),   // title of screen
      ),
      'input' => array(
          array(
              'type' => 'checkbox',   // type is checkbox type
              'label' => $this->l('Sortgroup Elements'),   // name of input block on screen
              'name' => 'selectedElements',
              'multiple' => true,
              'values' => array(
                  'query' => Sortgroup::getElements($default_lang),  // get values to show
                  'id' => 'id_feature',  // field that is used for return value(s)
                  'name' => 'name'  // field used to display (feature name in my case)
              ),
              'hint' => $this->l('Select the sortgroups you want to use for sorting of your products.')
          ),
      ),
      'submit' => array(
          'title' => $this->l('Save'),
          'class' => 'button'
      )
  );
     
  $helper = new HelperForm();
     
  // Module, token and currentIndex
  $helper->module = $this;
  $helper->name_controller = $this->name;
  $helper->token = Tools::getAdminTokenLite('AdminModules');
  $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
     
  // Language
  $helper->default_form_language = $default_lang;
  $helper->allow_employee_form_lang = $default_lang;
     
  // Title and toolbar
  $helper->title = $this->displayName;
  $helper->show_toolbar = true;        // false -> remove toolbar
  $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
  $helper->submit_action = 'submit'.$this->name;
  $helper->toolbar_btn = array(
      'save' => array(
          'desc' => $this->l('Save'),
          'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
          '&token='.Tools::getAdminTokenLite('AdminModules'),
      ),
      'back' => array(
          'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
          'desc' => $this->l('Back to list')
      )
  );
     
  // Load currently selected values from ps_configuration table
  $storedFeatures = explode('|', Configuration::get('PS_FEATURESORT_FEATURES_SELECTED'));
  $arrayIndex = 0;
  foreach (Feature::getFeatures($default_lang) as $feature) {
    $arrayIndex++;
    $helper->fields_value['selectedFeatures_'.$arrayIndex] = ($storedFeatures[$arrayIndex-1] != '' ? true: false);
  }
  return $helper->generateForm($fields_form);
}
 
 
 
Hope this gives some idea how to use the checkbox.
 
pascal
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...