从页面上的链接获取ID

时间:2016-04-06 09:08:01

标签: javascript php jquery pdf moodle

我的Moodle有两页。第一个是注册页面,第二个是课程页面。我们每个人都有一个按钮,可以打印 PDF 。在注册页面上有面包屑,如下所示:

  

Startpage->课程 - >杂项 - >学习课程   1->注册 - >注册选项

Learning Course 1下有一个链接,即:

  

http://localhost/mymoodle/course/view.php?id=6

如何从链接获取 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
  

http://localhost/mymoodle/enrol/index.php?id=6

它不会起作用。

需要 id 才能从pdf的课程中获取信息,其中包含:

$courseid = required_param('id', PARAM_INT);

注册页面只是加载而且PDF没有打印,所以我猜PDF不会从课程中获取ID?我是Moodle和Javascript的新手,所以任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用Moodle单键功能代替Javascript。

$printpdfurl = new moodle_url('/local/getinfo/getinfocourse.php', array('id' => $id));
echo $OUTPUT->single_button($printpdfurl, get_string('printpdf', 'local_getinfo'));

答案 1 :(得分:0)

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