I have two pages in my Moodle. The first is the enrolment page and the second is the course page. On each of them I have a button, which prints PDF. On the enrolment page there are breadcrumbs, which look like that:
Startpage->Course->Miscellaneous->Learning Course
1->Enrolment->Enrolment Options
Under Learning Course 1 there is a link, which is:
http://localhost/mymoodle/course/view.php?id=6
How to get the id from this link? I need the id in order to get the course information into PDF.
I build up the functionality to get the id on course level and the code works:
$('#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']
});
When trying to get the id it from the enrolment url
http://localhost/mymoodle/enrol/index.php?id=6
It won't work.
The id is needed in order to get the information from the course for the pdf, where there is:
$courseid = required_param('id', PARAM_INT);
The enrolment page just loads and the PDF isn't being printed, so I guess the PDF doesn't get the id from the course? I am new to Moodle and Javascript, so any help will be appreciated.
You could use the Moodle single button function instead of Javascript.
$printpdfurl = new moodle_url('/local/getinfo/getinfocourse.php', array('id' => $id));
echo $OUTPUT->single_button($printpdfurl, get_string('printpdf', 'local_getinfo'));
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
Related
Been trying for a while to send variables using url like this:
$.post("Controlador.php",{Ema:email_usuario, Cont:pass_usuario,Acc: acc},
function(datos){
json = JSON.parse(datos);
var Nombres = json.Nombres;
var Cedula = json.Cedula;
location.href = "Acerca.html proced="+Nombres+"&proce="+Cedula;
Now, it actually works sending the variables into the new page as shown here, but I can't seem to find a way to use these variables and I was wondering if anyone could help out. Thank you.
Edit: Alright, I managed to make it work... slightly (Still very new at this). I managed to use the variables (in this case the name "Martin") and display it almost as intended. This was the result. As you can see, it has a weird %20 which I don't know how it got there. Any ideas about how to remove it? Thanks a lot. Below I display what I used for this:
<script>
function getUrlVars(){
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];
}
return vars;
}
//Calling function gerUrlVars to get value from url
function volar(){
var proce= getUrlVars()["proce"];
var proced= getUrlVars()["proced"];
document.getElementById("sirve").innerHTML = proced;
}
</script>
</head>
<body onload="volar();">
get value from URL
function getUrlVars()
{
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];
}
return vars;
}
//Calling function gerUrlVars to get value from url
var proce= getUrlVars()["proce"];
var proced= getUrlVars()["proced"];
Now you can use proce and proced .
I think this is simple but can't seem to find the answer.
In our autoresponder, when a contact clicks a link to visit a page, the contact id and email are passed through to the next page.
So by clicking an email, the contact lands on page.com/1/?id=123&email=name#gmail.com
On that page, and there is a button to click to go to the next page...
what do I need to do so the parameters pass to the next page and the contact lands on page.com/2/?id=123&email=name#gmail.com?
Thanks for the help!
If the page number is all that changes:
button.addEventListener('click', () => {
const currentPage = Number(window.location.href.match(/\d+/));
window.location.href = window.location.href.replace(/\d+/, currentPage + 1);
});
Edit: here is a function to get the querystring parameters
from This link
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
If you already know the base url, and have the parameters just combine the two and redirect.
document.getElementById("myButton").onclick = function () {
var url = "page.com/2"; //get your base url how ever you're doing it.
var queryString = "?id=123&email=name#gmail.com";
var fullUrl = url + queryString;
window.location.href = fullUrl ;
};
I have a url like this
http://localhost:9000/index.html#/acceptRequest?screen_name=kailash&rf=FIN1
Here I want to take FIN1 from the url and store it in another variable
I am using doing this in angularjs
I have made a function like this
function getUrlVars()
{
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];
console.log(hash[1]);
}
return vars;
}
Here the console line hash[1] prints kailash FIN1.I only need the FIN1 to be stored and just print it in the console.How to make that happen?...
Anyway thanks in advance...
If the URL is for a resource in your Angular app, you can simply inject the $location service and use
var anotherVariable = $location.search().rf
See https://docs.angularjs.org/api/ng/service/$location#search
Here is simple solution for you.In core Javascript.
var url = "http://localhost:9000/index.html#/acceptRequestscreen_name=kailash&rf=FIN1";
var test = url.lastIndexOf('=');
var r = url.slice(test+1);
alert(r);
This code will get the url parameter you need. You just have to call it and tell what value you need.
function getUrlParameter(name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results == null) {
return null;
} else {
return results[1] || 0;
}
};
Use it like
var yourVariable= getUrlParameter("rf");
It will return the value of rf.
Hope this helps.
After some research i got an answer
we just have to store the return value of that function to a variable like this
var urlParams = getUrlVars();
then all we need to do is to
console.log(urlParams['rf']);
thats it...
anyway you guys have been a great help... Thank you for responding.
I'm trying to get url query string that comes back from the security API, so my URL looks like this www.mysite.com/profile#code=xxxx&id_token=xxx I'd like to get the values of code and id_token.
I tried using activatedRoute in my ngOnInit() but params is undefined. Can someone shed a light if this is the right way? I have created a function but its not an angular way of getting the URL query string.
this.routeParam.queryParams
.subscribe(params => {
let code = params['id_token'];
console.log('code val >> ', code);
})
My attempt which works but I'd like to do it in angular way.
getURLVars(key) {
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];
}
return vars[key];
}
Try this:
Using snapshot you can get the query string from URL.
ngOnInit() {
let code = this.router.snapshot.queryParams['id_token'];
}
I have an URL like http://www.test.pl/product-pol-4406.html
With Geasemonkey scripts I want to get the "4406" part from the URL, but I don`t have any idea how to do it. My code is:
var input=document.createElement("input");
input.type="button";
input.value="Edytuj";
input.alt="visitPage";
input.onclick = visitPage;
input.setAttribute("style", "font- size:18px;position:absolute;top:120px;right:40px;");
document.body.appendChild(input);
function visitPage(){
window.location='https://test.pl/panel/product-edit.php?idt=4406';
}
Any suggestions? Please.
use below function to get your 'idt' value in javascript
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var result= = getParameterByName('idt');
Just use
var url = window.location.href;
To get the full URL. According to your needs you could parse the URL with regex afterwards to get what you need, or e.g. split it to get parts of it with a seperator that makes sense with the type of URLs you are working with.
If you want to take the product id from the given url and then pass to the php page. You can try this
var url = "http://www.test.pl/product-pol-4406.html" //window.location.href
if(url){
url = url.substr(url.lastIndexOf("/") + 1);
var productID = url.match(/\d+/);
alert(productID);
}
Update:
function getProductID(){
var url = "http://www.test.pl/product-pol-4406.html" //window.location.href
if(url){
url = url.substr(url.lastIndexOf("/") + 1);
return url.match(/\d+/);
}
}
And then call the function inside the visitePage() function
function visitPage(){
var productID = getProductID();
if(productID){
window.location='https://test.pl/panel/product-edit.php?idt='+productID;
}
}