execute custom function after clicking a button

Re: execute custom function after clicking a button

by Mark Johnson -
Number of replies: 0
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Andreas,

Moodle's architecture is fairly typical of a PHP web app, in that in order to execute a function, you send a request to a URL of a .php file on the webserver, which contains the function call.

To do this in a synchronous manner (which will reload the page after you click your button), you need to either have a link to the URL as per your example above (which will be a GET request), or create a form containing the button where the form's action attribute contains the url (which will be a POST request). If your function will change data in the system, you should make a POST request, then redirect back to the original page (to prevent the page being reloaded and repeating the change).

To do this in an asynchronous manner (which will not reload the page after you click the button), you'll need to implement a web service then write a Javascript module to call it via an AJAX request when the button is clicked.

Average of ratings: Useful (1)