General developer forum

Create/handle button in view.php

Picture of Peter Bri
Re: Create/handle button in view.php

Hi Richard

yes, I am just trying to write a simple activity module, which in frist step only displays a button and executes a DB query on button click. The result of the query should be displayed on the same page as the button - in a div or something.

Because the button is clicked on client side, I thought JS should be used to handle it. I created an AMD module as shown in your example but then got stuck in ajax and webservice stuff.

Generally I'm not sure when JS should be used and how ajax is tied to webservices in Moodle. I read Moodle should work when JS is disabled in client browser.

Thanks a lot for your help.


Average of ratings: -
Richard Jones
Re: Create/handle button in view.php
Particularly helpful MoodlersPlugin developersTesters

Here's what I can suggest:

1. Download moodle-mod_NEWMODULE from Moodle HQ's GitHub account.  

2.  Follow the instructions carefully and test that it installs and works.

3. Near the lines 'Yay, it works!' add something like this:

// Replace the following lines with you own code.
echo $OUTPUT->heading('Yay! It works!');
$link = new moodle_url('/mod/widget/view.php', ['id' => $cm->id]);
echo html_writer::link($link, get_string('click', 'mod_widget'), ['class' => 'btn btn-primary']);

You will have a button that sends you back to the view.php page (ie the one you are already on).  What you could then do is copy and rename the view.php page and modify it to display data you obtain from the database, say in a table.  

Other html_writer functions can be found here (or browse the source code):

Hope this will help you get started.


Average of ratings: -