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: -
stdClass Object
(
[submitbutton] => Submit
)
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?
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);
}
}}
Complete form code: -
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;
}
}
}
SOLVED! I found that the extra parameter I was passing in the URL when the page is first loaded and the form is first created also needed to be present when the form is submitted, so the extra parameter on the url searched for a database record, and when I submitted the form, the page gets reloaded and all the functions get called again and fails because the parameter is missing and the records are not found. Hope this helps someone else.