1

I want to create a parameter for plugin that will be read from and stored in my own table. I use a custom form field type, but I do not know where to add the code.

<?php
    defined('_JEXEC') or die('Restricted access');
    jimport('joomla.form.formfield');

    class JFormFieldMyOptions extends JFormField {

        protected $type = 'myOptions';

        public function getInput() {
              return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                   '<option value="1" >Option 1</option>'.
                   '<option value="2" >Option 2</option>'.
                   '<option value="3" >Option 3</option>'.
                   '</select>';
        }
    }
Tomasz
  • 41
  • 5

1 Answers1

1

I think about the following solution.

  • You should include JavaScript to your page.
  • You should attach JavaScript event to the form. When you submit the form, you will get the option value and will send it to your controller via AJAX. That script will store the value in your database. You should use RAW controller.

    class JFormFieldMyOptions extends JFormField {
    
        protected $type = 'myOptions';
    
        public function getInput() {
              return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                   '<option value="1" >Option 1</option>'.
                   '<option value="2" >Option 2</option>'.
                   '<option value="3" >Option 3</option>'.
                   '</select>';
        }
    
        $document = JFactory::getDocument();
    
        $document->addScriptDeclaration('
             jQuery(document).ready(function() {
    
                jQuery("#form_id").on("submit", function(event) {
    
                   var optionValue = jQuery("#'.$this->id.'").val();
    
                   // Send the option value to your script via AJAX.
                   // ....
    
                   // Submit the form.
                   jQuery("#form_id").submit();
                });
    
            });
       ');
    }}
    
Community
  • 1
  • 1
  • Thank you for your answer. If forms id is always the same regardless of the backend template and Joomla version? – Tomasz Apr 24 '16 at 12:59