title: Moodle Quickform adding elements in foreach loop not submitting
body: <p>I am making a form from database fields, so I pull all the records and loop through and add the form elements in a foreach loop in php. The problem is when I submit the form the elements are not posted, the only return I get is the submit button: - </p>
  
  <pre><code>stdClass Object
  (
      [submitbutton] =&gt; Submit
  )
  </code></pre>
  
  <p>This is how I create the elements, these all display and fucntion correctly onscreen, it just does not post when I submit, but the elements do post if I don't have them in the foreach loop, but I need to create them dynamically from the database, any ideas?</p>
  
  <pre><code>foreach($records as $log){
          $inc++;
  
          if($log-&gt;type == 0){ 
  
              $mform-&gt;addElement('html', '&lt;p&gt;'.$log-&gt;leadin.'&lt;/p&gt;');
  
              $attributes = array();
              $distractors = explode(',', $log-&gt;distractors);
              $radioarray=array();
              $count = 0;
  
              foreach($distractors as $dis){
                  $count++;
                  $radioarray[] =&amp; $mform-&gt;createElement('radio', 'radio', '', $dis, $count, array());
              }
  
              $mform-&gt;addGroup($radioarray, 'radioar'.$inc, '', array(' '), false);
          }
          else if($log-&gt;type == 1){
  
              $mform-&gt;addElement('html', '&lt;div&gt;'.$log-&gt;leadin.'&lt;/div&gt;');
  
              $distractors = explode(',', $log-&gt;distractors);
              $count = 0;
  
              foreach($distractors as $dis){
                  $count++;
                  $mform-&gt;addElement('checkbox', 'check'.$count, $dis);
              }
          }}
  </code></pre>
  
  <p>Complete form code: - </p>
  
  <pre><code>class build_user_survey extends moodleform{
  public function definition() {
      global $CFG;
      global $DB;
      global $OUTPUT;
  
      $mform =&amp; $this-&gt;_form;
      $errors= array();
      $br = "&lt;br /&gt;";
      $select = '';
      $records = $this-&gt;_customdata['thequestions'];
      $inc = 0;
      $attributes=array('rows'=&gt;'10','cols'=&gt;'80');
      $mform-&gt;addElement('hidden', 'id');
      $mform-&gt;setType('id', PARAM_INT);
      $mform-&gt;addElement('hidden', 'viewpage');
      $mform-&gt;setType('viewpage', PARAM_INT);
      $mform-&gt;addElement('hidden', 'pickedsurvey');
      $mform-&gt;setType('pickedsurvey', PARAM_INT);
      $mform-&gt;addElement('hidden', 'questiontype');
      $mform-&gt;setType('questiontype', PARAM_INT);
  
  
       foreach($records as $log){
          $inc++;
  
          if($log-&gt;type == 0){ 
  
              $mform-&gt;addElement('html', '&lt;div&gt;'.$log-&gt;leadin.'&lt;/div&gt;');
  
              $distractors = explode(',', $log-&gt;distractors);
              $count = 0;
  
              foreach($distractors as $dis){
                  $count++;
                  $mform-&gt;addElement('radio', 'radio'.$inc, '', $dis, $count, array());
              }
          }
          else if($log-&gt;type == 1){
  
              $mform-&gt;addElement('html', '&lt;div&gt;'.$log-&gt;leadin.'&lt;/div&gt;');
  
              $distractors = explode(',', $log-&gt;distractors);
              $count = 0;
  
              foreach($distractors as $dis){
                  $count++;
                  $mform-&gt;addElement('checkbox', 'check'.$count, $dis);
              }
          }
          else if($log-&gt;type == 2){
              echo "&lt;script type='text/javascript'&gt;alert('here');&lt;/script&gt;";
              $thename = 'answer';
              $mform-&gt;addElement('textarea', $thename, $log-&gt;leadin, $attributes);
          }  
      } 
     foreach($records as $log){
          $mform-&gt;addElement('radio', 'radio', '', 'ioh;', 0, array());
     }
  
      $mform-&gt;addElement('textarea', 'answerWorking', '$log-&gt;leadin', $attributes);
      $this-&gt;add_action_buttons($cancel = true, $submitlabel='Submit');
  }
  public function validation($data, $files) {
      global $DB;
      $errors= array();
      if($data['id']) {
          return true;
      }
  }
  </code></pre>
  
  <p>}</p>