General developer forum

Get value of selected dropdown item

 
Picture of Peter Bri
Get value of selected dropdown item
 

Hi,

I'm developing a small activity plugin and have some issues with querying a selected dropdown item.

Dropdown on page 1:

echo html_writer::select($myarray, "mydropdown");

By clicking on a button on page 1 a new page 2 opens where I need the value of the select item in mydropdown

$myurl= new moodle_url('/mod/test/newpage.php', ['id' => $cm->id, 'dropdownvalue' => ?value of dropdown?]);
echo html_writer::link($myurl, "Go to next page", ['class' => 'btn btn-primary']);

Is there any way to get the value of the item selected by the user in the dropdown? Is JavaScript needed?

Kind regards

Peter

 
Average of ratings: -
Picture of Mark Johnson
Re: Get value of selected dropdown item
Core developersParticularly helpful MoodlersPlugin developers

If you wrap your select and button in a <form> element, with the URL of the script that will process the form in the element's action attribute, the value will be sent to that script, and you will be able to read it with required_param().  Note, you need to use an <input type="submit"> for the submit button for this to work, not a link.

You can also use Moodle's form API to take care of the form structure for you.

 
Average of ratings: -
Picture of Peter Bri
Re: Get value of selected dropdown item
 

Thank you Mark. Forms was the cue I was looking for.

 
Average of ratings: -