Use of "advcheckbox" in a form to get selected "id"

Use of "advcheckbox" in a form to get selected "id"

by EAS CHINTA -
Number of replies: 7

I am trying to build a form with a few options from my database and I am doing so with a form. 

I want to select something and then when I click on the button to have the id of the selected item shown along with the name of it in order to "confirm" my choice. 

This is the code I have so far: 

$mform->addElement('advcheckbox', 'dates[]', null, null, array(0, $id)); // this is my form file. 

$url = new moodle_url('/mod/clinic/view.php?id='.$cm->id);
$form = new clinicform($url); // these two lines are in my view.php file.


Right now, when I select one of my options I do not get that selected item but I get this: 

object(stdClass)#1447 (1) { ["dates"]=> array(1) { [""]=> int(0) } }

Does anyone know how to use the checkbox feature for single and multiple selection and then confirmation of the them too?

Average of ratings: -
In reply to EAS CHINTA

Re: Use of "advcheckbox" in a form to get selected "id"

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
In reply to Renaat Debleu

Re: Use of "advcheckbox" in a form to get selected "id"

by EAS CHINTA -
Hi Renaat.
I have checked the documentation, unfortunately it hasn’t been very helpful in my case.
In reply to EAS CHINTA

Re: Use of "advcheckbox" in a form to get selected "id"

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
In the documentation I see
$mform->addElement('advcheckbox', 'ratingtime', get_string('ratingtime', 'forum'), 'Label displayed after checkbox', ['group' => 1], [0, 1]);

Your code is
$mform->addElement('advcheckbox', 'dates[]', null, null, [0, $id]);

When I compare both lines:
  • dates[]: seems to me a needless test to see if field names can contain special characters like [ and ]
  • null: you do not provide a label
  • null: you do not provide text to be shown after the checkbox
  • first array should be an array of attributes, you provide an array of values
  • second array:  you change the optional values [0,1] into [0,???] , which can also be [0,0]

 

 

 
Average of ratings: Useful (1)
In reply to Renaat Debleu

Re: Use of "advcheckbox" in a form to get selected "id"

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Renaat is not quite correct in what he has written.

  • dates[] has nothing to do with trying to see if a field can contain special characters, but a quite legitimate attempt to return an array of results from the form. The reason it doesn't work is that all the elements end up with the same index once the Moodle form processing has taken place - what you need to do is use the $id in the name - i.e. "dates[$id]"
  • null / null: you should provide a label either before or after the checkbox, otherwise users have no way of knowing what they are ticking (but that isn't the reason the submission isn't working)
  • first array: agreed, that is meant to be the attributes to apply, not the values
  • second array: agreed, that is where you should define the value of the checkbox

So the solution would look something like this:

$mform->addElement('advcheckbox', 'dates[6]', null, 'Opt 1', [], [0, 2]);

In this case - I've used '6' and '2' as arbitrary values, you could also write:

$mform->addElement('advcheckbox', "dates[$id]", null, 'Opt 1', [], [0, $id]);
(on submission, this would look like: [2 => 2, 3 => 0, 4 => 0, 5 => 5], assuming that 2 + 5 were checked)

or you could simply use:

$mform->addElement('advcheckbox', "dates[$id]", null, 'Opt 1');

(on submission, this would look like: [2 => 1, 3 => 0, 4 => 0, 5 => 1], assuming 2 + 5 were checked)

If you want the unchecked values to be excluded from the submission completely, then use 'checkbox' instead of 'advcheckbox' (which uses hidden fields to ensure that a value is always present for every item).




Average of ratings: Useful (1)
In reply to Davo Smith

Re: Use of "advcheckbox" in a form to get selected "id"

by EAS CHINTA -
Thank you both!
I had not included the 'Opt 1' as I don't need to have a label for the option since the option is supposed to be the item itself so they would know what they are choosing. 
I am sharing a screenshot to show you what it looks like at the moment. 

After trying your code Davo, it did change something but it still doesn't bring me the Date Time Location chosen as I needed. 
I tried this: 
$mform->addElement('advcheckbox', "dates[$id]", null, null, [], [0, $id]);



It brings this: 
object(stdClass)#1463 (1) { ["dates"]=> array(2) { [4]=> string(1) "0" [8]=> string(1) "8" } } 

In reply to EAS CHINTA

Re: Use of "advcheckbox" in a form to get selected "id"

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
That looks like it produces exactly what I'd expect it to.

It has an array, indexed by the ids, with 0 values for items not selected and values matching the id for those that are selected.

If you want the date/time, then you've got 2 options:

  • Use a DB query to turn the id back into the database record it refers to
  • Set the 'value' to the timestamp - e.g. $mform->addElement('advcheckbox', "dates[$id]", null, null, [], [0, $timestamp]);

The second option will return something like:

$data->dates = [4 => 0, 8 => 125623464]


Average of ratings: Useful (1)
In reply to Davo Smith

Re: Use of "advcheckbox" in a form to get selected "id"

by EAS CHINTA -
Thank you again Davo. Seems like you know you way around Moodle pretty well.