从页面上的 link 获取 ID
Get ID from link on the page
我的 Moodle 中有两个页面。第一个是 注册页面 ,第二个是 课程页面 。在他们每个人身上,我都有一个 按钮 ,它打印 PDF。在注册页面上有 面包屑,看起来像这样:
Startpage->Course->Miscellaneous->Learning Course
1->Enrolment->Enrolment Options
Learning Course 1
下面有一个link,就是:
如何从这个link中得到id?我需要 id
才能将 课程信息 转换为 PDF。
我构建了在课程级别获取 id 的功能并且代码有效:
$('#page-enrol-index .printpdf').click(function() {
//jquery extend function
$.extend(
{
redirectPost: function(location, args)
{
var form = '';
$.each( args, function( key, value ) {
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
}
});
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
//create the pdf
$.redirectPost("http://localhost/mymoodle/local/getinfo/getinfocourse.php", {
id: vars['id']
});
当试图从 注册中获取 id 时 url
不行。
需要 id 才能从课程中获取 pdf 的信息,其中有:
$courseid = required_param('id', PARAM_INT);
注册页面刚刚加载,但未打印 PDF,所以我猜 PDF 没有从课程中获取 ID?我是 Moodle 和 Javascript 的新手,因此我们将不胜感激。
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// query string: 'http://localhost/mymoodle/course/view.php?id=6'
var id = getParameterByName('id'); // 6
您可以使用 Moodle 单按钮功能代替 Javascript。
$printpdfurl = new moodle_url('/local/getinfo/getinfocourse.php', array('id' => $id));
echo $OUTPUT->single_button($printpdfurl, get_string('printpdf', 'local_getinfo'));
我的 Moodle 中有两个页面。第一个是 注册页面 ,第二个是 课程页面 。在他们每个人身上,我都有一个 按钮 ,它打印 PDF。在注册页面上有 面包屑,看起来像这样:
Startpage->Course->Miscellaneous->Learning Course 1->Enrolment->Enrolment Options
Learning Course 1
下面有一个link,就是:
如何从这个link中得到id?我需要 id
才能将 课程信息 转换为 PDF。
我构建了在课程级别获取 id 的功能并且代码有效:
$('#page-enrol-index .printpdf').click(function() {
//jquery extend function
$.extend(
{
redirectPost: function(location, args)
{
var form = '';
$.each( args, function( key, value ) {
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
}
});
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
//create the pdf
$.redirectPost("http://localhost/mymoodle/local/getinfo/getinfocourse.php", {
id: vars['id']
});
当试图从 注册中获取 id 时 url
不行。
需要 id 才能从课程中获取 pdf 的信息,其中有:
$courseid = required_param('id', PARAM_INT);
注册页面刚刚加载,但未打印 PDF,所以我猜 PDF 没有从课程中获取 ID?我是 Moodle 和 Javascript 的新手,因此我们将不胜感激。
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// query string: 'http://localhost/mymoodle/course/view.php?id=6'
var id = getParameterByName('id'); // 6
您可以使用 Moodle 单按钮功能代替 Javascript。
$printpdfurl = new moodle_url('/local/getinfo/getinfocourse.php', array('id' => $id));
echo $OUTPUT->single_button($printpdfurl, get_string('printpdf', 'local_getinfo'));