How to get selected Item from a dropdown

How to get selected Item from a dropdown

by Mojalefa Jonas Malatji -
Number of replies: 6

How to get selected Item from a dropdown/select/selectyesno in moodle

Average of ratings: -
In reply to Mojalefa Jonas Malatji

Re: How to get selected Item from a dropdown

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Do you mean an element that has been submitted as part of a Moodle form?

Or do you mean a standalone 'single-select' (that redirects to a new page when selected)?

Or do you mean 'how do I access this via javascript on the page'?

Or do you mean 'how do I get the value of a setting I added to the global settings page'?

Please describe exactly what you are doing and someone should then be able to give you some help.

In reply to Davo Smith

Re: How to get selected Item from a dropdown

by Mojalefa Jonas Malatji -

I need to display info on the block based on the value selected from the select. What I mean is that, I have created a custom block and I need to run the query to pull record from the quiz so that it can be displayed on the block, 

e.g

//This is the selectyesno which I need to get a selected value from

$mform->addElement('selectyesno', 'my_yes_no', get_string('my_yes_no_order', 'quiz'));


// The $selectedItem should be the value fro the selectyesno

   e.g $DB->get_record_sql('SELECT * FROM {quiz} WHERE flag = :flag', array('flag' => $selectedItem), $strictness=IGNORE_MULTIPLE);


...Thanks in advance


In reply to Mojalefa Jonas Malatji

Re: How to get selected Item from a dropdown

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If this is in a block and the form you edited was the instance settings form for the block, then you should be able to write:

$selecteditem = $this->config->my_yes_no;

(As an aside, you should avoid names with underscores in them, Moodle code guidelines would have 'myyesno' as the name).


In reply to Davo Smith

Re: How to get selected Item from a dropdown

by Mojalefa Jonas Malatji -

No the selectyesno is in  mod_quiz_mod_form class and I need to get the selected value from this class and pass it to the block 

In reply to Mojalefa Jonas Malatji

Re: How to get selected Item from a dropdown

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, there isn't a my_yes_no setting in mod_quiz_mod_form, so I can only assume this is a local modification to the quiz module.

If you have made a local change there, then you also need to update mod/quiz/db/install.xml + mod/quiz/db/upgrade.php to add somewhere to store the results of this setting, then you need to check the functions quiz_add_instance and quiz_update_instance in mod/quiz/lib.php to make sure this setting is being saved into these new DB fields (which it may be already). Then you can look in the table mdl_quiz to see the value that was inserted when the quiz was saved.

As always, you will find plugins a lot easier to maintain if they don't involve making changes to the core Moodle code (and apologies if I've misunderstood and you haven't done that).