Forms Assistance - Values

Forms Assistance - Values

by Frank Erazo -
Number of replies: 1

Good afternoon, fellow Moodlers.  

I have created a form with a select, radio buttons and submit button on a page called mystaff.php.  When I select my choices and click the button, the page remains on mystaff.php.  

How can I pass the values?  Right now, I'm trying to keep things simple by working with the select option.  

Here's the code for the select: $mform->addElement('select', 'course', get_string('courses', 'history'), $reqcourses);

At the top of my page (mystaff.php), I've placed the following:

$course= optional_param('course', '', PARAM_TEXT); // course number from dropdown menu

I am trying to echo the value, but I keep getting an error message.  How do I properly pass the values so I can generate a report?

Thank you.

Average of ratings: -
In reply to Frank Erazo

Re: Forms Assistance - Values

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Frank,

If I can refer to my 'set_layout.php' from the prevous post, then the input data to the form is in the 'new', but to get the data out when the 'submit' button is pressed then you need to call 'get_data()', now for some reason and I do not know why, this needs to be done before the form is 'display()'ed as when you click on 'submit' it returns back to the code, in your case 'mystaff.php'.  But my code looks like this:

$mform = new set_layout_form(null, array('courseid' => $courseid, 'setelement' => $setelement, 'setstructure' => $setstructure));

if ($mform->is_cancelled()) {
redirect($courseurl);
} else if ($formdata = $mform->get_data()) {
put_layout($formdata->id, $formdata->set_element, $formdata->set_structure);
redirect($courseurl);
}

echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox');
$mform->display();
echo $OUTPUT->box_end();
echo $OUTPUT->footer();

So the return value names of '$formdata->set_element' and '$formdata->set_structure' are the names defined when the elements were added in 'addElement' - which in my case is in 'set_layout_form.php' as:

$mform->addElement('select', 'set_element', get_string('setlayoutelements', 'format_topcoll'), $formcourselayoutelements);

and

$mform->addElement('select', 'set_structure', get_string('setlayoutstructure', 'format_topcoll'), $formcourselayoutstrutures); 

where I defined 'set_element' and 'set_structure' and 'put_layout' is my function but is being passed in the values of the drop down.  So if the form returns data then 'get_data()' will work and that will be a structure containing the element values you want.

I hope this makes sence, cheers,

Gareth