Select a specific value in select tag

Select a specific value in select tag

by Daniele Caldelli -
Number of replies: 12

Hello all,

I created e select tag with the specific call $mform->addElement('select', 'level', get_string('level'), $select_array); and I obtain for example:

<select name="level" id="id_level">
<option value="a">A</option>
  <option value="b">B</option>
  <option value="c">C</option>

<option value="d">D</option>
</select>

Everything work fine, but I need to select one option depending of the value, like this:

<select name="level" id="id_level">
<option value="a">A</option> <option value
="b">B</option> <option value="c" selected="selected">C</option>
<option value="d">D</option>
</select>


How can I do that?!?

Thanks to all!!!

Average of ratings: -
In reply to Daniele Caldelli

Re: Select a specific value in select tag

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Off the top of my head: $mform->setDefault('level', 'c');
In reply to Davo Smith

Re: Select a specific value in select tag

by Daniele Caldelli -

Hi Davo,

I tried this solution, but it doesn't work. Have you any idea?!? I post my code:

$mform->addElement('select', 'level', get_string('level'), $select_array);
$mform->setDefault('level', "2.1.2");

My select tab is made like it:

<select name="level" id="id_level">
<option value="1.0.1">1. Titolo 1</option>
  <option value="2.1.2"> &nbsp; 1.1. Titolo 1.1.</option>
  <option value="0.0.3">
&nbsp; &nbsp; &gt;nuovo&lt;</option>
<option value="3.0.3">2. Titolo 2</option>
</select>


Do you think that the string 2.1.2 give some problem? Do you have any ideas?

Thanks

In reply to Daniele Caldelli

Re: Select a specific value in select tag

by Justin Wyllie -

Hi Daniele

I think maybe:

$myselect = $mform->addElement('select', 'level', get_string('level'), $select_array);

$myselect->setSelected('c'); //for your first example

Justin Wyllie

In reply to Justin Wyllie

Re: Select a specific value in select tag

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

Oopps... looks like 'setSelected' is the right answer, rather than 'setDefault' (in this case - setDefault works with most other form elements).

I've added 'setSelected' into the Moodle docs here: http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition#select

Please could someone double-check what I've written (as I'm typing without access to a Moodle dev server at this moment).

In reply to Davo Smith

Re: Select a specific value in select tag

by Justin Wyllie -

Hi Davo

I saw that and it looks good.

Do we need to mention that if setMultiple(true) is called then you can pass an array of values to setSelected() and each one will be selected?

On another point what difference does it make taking a reference to the form i.e. what is the difference between:

$myselect = &$mform->addElement("... and

$myselect = $mform->addElement(" ?

Thanks.

Justin

 

In reply to Justin Wyllie

Re: Select a specific value in select tag

by Daniele Caldelli -

Hi guys,

I tried this solution:

$myselect = $mform->addElement('select', 'level', get_string('level'), $select_array);
$myselect->setSelected('2.1.2');

then

$myselect = &$mform->addElement('select', 'level', get_string('level'), $select_array);
$myselect->setSelected('2.1.2');

then

$mform->addElement('select', 'level', get_string('level'), $select_array);
$mform->getElement('level')->setSelected('2.1.2');

but unfortunately nothing happens... then I make a little test. I try with:

$myselect = $mform->addElement('select', 'level', get_string('level'), $select_array);
$myselect->setMultiple(true);

And the setMultiple call work perfect... So, why the setSelected call doesn't work? I don't understand... I hope somebody can help me...

In reply to Daniele Caldelli

Re: Select a specific value in select tag

by Justin Wyllie -

Hi Daniele

I would expect:

$myselect = $mform->addElement('select', 'level', get_string('level'), $select_array);
$myselect->setSelected('2.1.2');

to work IF '2.1.2' is one of the values i.e. is one of the keys in $select_array.

I did  a test. This was my form definition code:

class modname_test extends moodleform {


public function definition() {
$mform  = $this->_form;
$myselect = $mform->addElement('select', 'level', 'label', array('a'=>'apple', 'b'=>'banana', '2.1.2'=>'2.1.2'));

$myselect->setSelected('2.1.2');

}

}

And this was my usage in a page:


require_once("local_forms.php");
$form = new modname_test(qualified_me());
$form->display();

It worked.

If it is not working for you I would check that the value you are trying to set is in the array by var_dumping $select_array.  Also on the web page I would actually see what selected had been set on - Firefox I find in particular can ignore the selected attribute if it is reloading the page from cache (at least I think that is the problem).

setMultiple of course does something different - it makes the select box a multi select one. In my experience you can pass setSelected an array in these cases.

Justin

In reply to Justin Wyllie

Re: Select a specific value in select tag

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I am sure setDefault works here - we use it on the quiz settings from.

Also, often what you want to do is use $form->setData() to set all the form fields to values loaded from the DB.

In reply to Tim Hunt

Re: Select a specific value in select tag

by Justin Wyllie -

Yes. This also works:

$myselect = $mform->addElement('select', 'level', 'label', array('a'=>'apple', 'b'=>'banana', '2.1.2'=>'2.1.2'));
$mform->setDefault('level', 'b');

And this:

$myselect->setMultiple(true);
$mform->setDefault('level', array('a', 'b'));

 

In reply to Justin Wyllie

Re: Select a specific value in select tag

by Russell England -
Picture of Plugin developers

Discovered that this doesn't work :

$myselect->setMultiple(true);
$defaults = array();
$defaults[] = 'a';
$defaults[] = 'b';
$mform->setDefault('level', $defaults);

But this does :

$myselect->setMultiple(true;)
$defaults = array();
$defaults['a'] = 'a';
$defaults['b'] = 'b';
$mform->setDefault('level', $defaults);

 

In reply to Tim Hunt

Re: Select a specific value in select tag

by Daniele Caldelli -

Hi all,

thanks a lot to everybody for all the help. Finally I solve the problem. The error wasn't the call $mform->setDefault('level', '2.1.2'); but that after this call I make $this->set_data($entity);

This second call transform again my select tab and cancel the selected attribute. Executing the setDefault after the set_data I solve the problem.

Thanks again!!!

In reply to Tim Hunt

Re: Select a specific value in select tag

by Chetan Sharma -

Hey Tim,

I want to call list of courses in which a user is enrolled to a different site ...What should I do?

I have called the username and user pic earlier with the code

<?php
require_once("../../config.php") ;
require_login(); //Won't do any good to 'get' a username 'til sombody's logged in.

echo $USER->username;
echo $USER->firstname; // and so on..


print_user_picture($USER, $course->id, $USER->picture, true, false, false);

 

Please Help me with calling the my courses list.