I'm fairly new to moodle and php in general.
I created a new block which acts like a display for custom events and reminder.
Inside that block is a button that should open the page of another plugin I created when clicked.
But I can't manage to set the button with a link that opens the new page.
When I press the button, nothing happens.
If I enter the address of the page of the other plugin in the address bar, it opens as expected.
global $CFG;
global $DB;
$mform = $this->_form;
$reminderinfo='';
$reminder = $DB->get_records('reminder_events');
foreach ($reminder as $remind){
$reminderinfo .= $remind->eventname . ' ' . $remind->time . '<br>';
}
$manageurl = $CFG->wwwroot . '/local/reminder/manage.php';
// Add reminderinfo to mform
$mform->addElement('html', '<input type="button" class="btn btn-primary" value="Add Reminder" onclick=\"location.href="'.$manageurl.'"\">');
My Answer:
It looks like you are trying to add a button to your block that links to the page of another plugin. The code you provided seems to be mostly correct, but there might be an issue with the way you are constructing the onclick event.
Try updating your code to the following:
php
global $CFG;
global $DB;
$mform = $this->_form;
$reminderinfo='';
$reminder = $DB->get_records('reminder_events');
foreach ($reminder as $remind){
$reminderinfo .= $remind->eventname . ' ' . $remind->time . '
';
}
$manageurl = $CFG->wwwroot . '/local/reminder/manage.php';
// Add reminderinfo to mform
$mform->addElement('html', '');
This code should properly construct the onclick event for the button to redirect to the specified manageurl when clicked. Make sure to clear your cache and refresh the page after making this change to see if the button now works as expected.
Rate this post
4 of 5 based on 3091 votesComments