【问题标题】:Moodle Quickform adding elements in foreach loop not submittingMoodle Quickform在foreach循环中添加元素未提交
【发布时间】:2014-03-14 19:31:43
【问题描述】:

我正在从数据库字段中创建一个表单,因此我提取所有记录并循环并在 php.ini 的 foreach 循环中添加表单元素。问题是当我提交表单时没有发布元素,我得到的唯一返回是提交按钮: -

stdClass Object
(
    [submitbutton] => Submit
)

这就是我创建元素的方式,这些元素都在屏幕上正确显示和运行,只是在我提交时它不会发布,但如果我在 foreach 循环中没有元素,它们会发布,但我需要创建从数据库中动态获取它们,有什么想法吗?

foreach($records as $log){
        $inc++;

        if($log->type == 0){ 

            $mform->addElement('html', '<p>'.$log->leadin.'</p>');

            $attributes = array();
            $distractors = explode(',', $log->distractors);
            $radioarray=array();
            $count = 0;

            foreach($distractors as $dis){
                $count++;
                $radioarray[] =& $mform->createElement('radio', 'radio', '', $dis, $count, array());
            }

            $mform->addGroup($radioarray, 'radioar'.$inc, '', array(' '), false);
        }
        else if($log->type == 1){

            $mform->addElement('html', '<div>'.$log->leadin.'</div>');

            $distractors = explode(',', $log->distractors);
            $count = 0;

            foreach($distractors as $dis){
                $count++;
                $mform->addElement('checkbox', 'check'.$count, $dis);
            }
        }}

完整的表格代码:-

class build_user_survey extends moodleform{
public function definition() {
    global $CFG;
    global $DB;
    global $OUTPUT;

    $mform =& $this->_form;
    $errors= array();
    $br = "<br />";
    $select = '';
    $records = $this->_customdata['thequestions'];
    $inc = 0;
    $attributes=array('rows'=>'10','cols'=>'80');
    $mform->addElement('hidden', 'id');
    $mform->setType('id', PARAM_INT);
    $mform->addElement('hidden', 'viewpage');
    $mform->setType('viewpage', PARAM_INT);
    $mform->addElement('hidden', 'pickedsurvey');
    $mform->setType('pickedsurvey', PARAM_INT);
    $mform->addElement('hidden', 'questiontype');
    $mform->setType('questiontype', PARAM_INT);


     foreach($records as $log){
        $inc++;

        if($log->type == 0){ 

            $mform->addElement('html', '<div>'.$log->leadin.'</div>');

            $distractors = explode(',', $log->distractors);
            $count = 0;

            foreach($distractors as $dis){
                $count++;
                $mform->addElement('radio', 'radio'.$inc, '', $dis, $count, array());
            }
        }
        else if($log->type == 1){

            $mform->addElement('html', '<div>'.$log->leadin.'</div>');

            $distractors = explode(',', $log->distractors);
            $count = 0;

            foreach($distractors as $dis){
                $count++;
                $mform->addElement('checkbox', 'check'.$count, $dis);
            }
        }
        else if($log->type == 2){
            echo "<script type='text/javascript'>alert('here');</script>";
            $thename = 'answer';
            $mform->addElement('textarea', $thename, $log->leadin, $attributes);
        }  
    } 
   foreach($records as $log){
        $mform->addElement('radio', 'radio', '', 'ioh;', 0, array());
   }

    $mform->addElement('textarea', 'answerWorking', '$log->leadin', $attributes);
    $this->add_action_buttons($cancel = true, $submitlabel='Submit');
}
public function validation($data, $files) {
    global $DB;
    $errors= array();
    if($data['id']) {
        return true;
    }
}

}

【问题讨论】:

    标签: php pear moodle quickform


    【解决方案1】:

    已解决!我发现我在第一次加载页面和第一次创建表单时在url中传递的额外参数也需要在提交表单时出现,所以url上的额外参数搜索了一条数据库记录,当我提交了表单,页面被重新加载,所有函数都被再次调用并失败,因为参数丢失并且找不到记录。希望这对其他人有帮助。

    【讨论】:

      【解决方案2】:

      复选框只有在被选中时才会返回一个值。

      如果您也想要未选择的值,请使用高级复选框

      $mform->addElement('advcheckbox', 'check'.$count, $dis);
      

      http://docs.moodle.org/dev/lib/formslib.php_Form_Definition#advcheckbox

      编辑:我刚刚复制了代码并将复选框更改为 advcheckbox 并且它可以工作:) 您还需要将 $count = 0 放在 for 循环之前。

      调用代码

      $thequestions = array();
      
      $thequestion = new stdClass();
      $thequestion->type = 1;
      $thequestion->leadin = 'leadin 1';
      $thequestion->distractors = 'dis1, dis2, dis3';
      $thequestions[] = $thequestion;
      
      $thequestion = new stdClass();
      $thequestion->type = 1;
      $thequestion->leadin = 'leadin 2';
      $thequestion->distractors = 'dis4, dis5, dis6';
      $thequestions[] = $thequestion;
      
      $mform = new build_user_survey(null, array('thequestions'=>$thequestions ));
      
      if ($data = $mform->get_data()) {
          var_dump($data);
      }
      $mform->display();
      

      变量转储

      object(stdClass)[3270]
        public 'id' => int 0
        public 'viewpage' => int 0
        public 'pickedsurvey' => int 0
        public 'questiontype' => int 0
        public 'check1' => string '0' (length=1)
        public 'check2' => string '0' (length=1)
        public 'check3' => string '0' (length=1)
        public 'check4' => string '0' (length=1)
        public 'check5' => string '0' (length=1)
        public 'check6' => string '0' (length=1)
        public 'radio' => string '0' (length=1)
        public 'answerWorking' => string '' (length=0)
        public 'submitbutton' => string 'Submit' (length=6)
      

      修改后的表单代码

      class build_user_survey extends moodleform {
          public function definition() {
      
      
              $mform =& $this->_form;
      
              $records = $this->_customdata['thequestions'];
              $inc = 0;
              $attributes=array('rows'=>'10','cols'=>'80');
              $mform->addElement('hidden', 'id');
              $mform->setType('id', PARAM_INT);
              $mform->addElement('hidden', 'viewpage');
              $mform->setType('viewpage', PARAM_INT);
              $mform->addElement('hidden', 'pickedsurvey');
              $mform->setType('pickedsurvey', PARAM_INT);
              $mform->addElement('hidden', 'questiontype');
              $mform->setType('questiontype', PARAM_INT);
      
              $count = 0;
               foreach($records as $log){
                  $inc++;
      
                  if($log->type == 0){
      
                      $mform->addElement('html', '<div>'.$log->leadin.'</div>');
      
                      $distractors = explode(',', $log->distractors);
      
                      foreach($distractors as $dis){
                          $count++;
                          $mform->addElement('radio', 'radio'.$inc, '', $dis, $count, array());
                      }
                  }
                  else if($log->type == 1){
      
                      $mform->addElement('html', '<div>'.$log->leadin.'</div>');
      
                      $distractors = explode(',', $log->distractors);
      
                      foreach($distractors as $dis){
                          $count++;
                          $mform->addElement('advcheckbox', 'check'.$count, $dis);
                      }
                  }
                  else if($log->type == 2){
                      echo "<script type='text/javascript'>alert('here');</script>";
                      $thename = 'answer';
                      $mform->addElement('textarea', $thename, $log->leadin, $attributes);
                  }
              }
             foreach($records as $log){
                  $mform->addElement('radio', 'radio', '', 'ioh;', 0, array());
             }
      
              $mform->addElement('textarea', 'answerWorking', '$log->leadin', $attributes);
              $this->add_action_buttons(true, 'Submit');
          }
      
      }
      

      【讨论】:

      • 是的,我已经尝试过了,但它似乎仍然无法正常工作,您是否看到或知道 $mform->addElement 在 foreach 循环中是否有效?当我在循环中使用它时,它会显示并且 html 会正确写入,但是当您提交 $form->get_data() 时仅返回提交按钮,当 $mform->addElement 不在 foreach 循环中时,它会返回原样应该怎么做,有什么想法吗?
      • 你能显示完整的表单代码吗?一件事是 count = 0 需要在 foreach ($records as $log) 循环之前设置,否则你会得到重复的元素名称。
      • 我已经添加了完整的表单代码,我用 $form = new build_user_survey(null, array('thequestions'=>$thequestions, 'thesurvey'=>$thesurvey )); 调用它。跨度>
      猜你喜欢
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      相关资源
      最近更新 更多