To do this you need:
- Javascript method
- ajax php file
- minor changes in your block
// Place this in new js file located in your block
function block_myblock_sendemail(e) {
e.preventDefault();
Y.log('Enetered method');
var ioconfig = {
method: 'POST',
data: {'sesskey' : M.cfg.sesskey},
on: {
success: function (o, response) {
//OK
var data = Y.JSON.parse(response.responseText);
if (data.result) {
alert('Result is OK!');
}
},
failure: function (o, response) {
alert('Not OK!');
}
}
};
Y.io(M.cfg.wwwroot + '/blocks/myblock/myajaxscript.php', ioconfig);
}
// Create new php file called myajaxscript.php and place it with your block
define('AJAX_SCRIPT', true);
require_once('/path/to/config.php');
require_login(null, false, null, false, true);
require_sesskey();
//Do your thing here
$result = true;
echo $OUTPUT->header();
echo json_encode(array('result' => $result));
// Finally add this to your get_content method of the block:
$this->page->requires->js('/blocks/myblock/myscript.js');
//some other code
$this->content->text = $OUTPUT->action_link('/blocks/myblock/myajaxscript.php', 'clickit', new component_action('click', 'block_myblock_sendemail'));