1

我正在从数据库字段中创建一个表单,因此我提取所有记录并循环并在 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;
    }
}

}

4

2 回答 2

0

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

于 2014-03-14T11:31:40.180 回答
0

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

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

$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');
    }

}
于 2014-03-13T11:21:59.057 回答