PDA

View Full Version : take JS vars and send to perl file.


bazz
01-08-2009, 05:25 PM
Hi,

I am already brain fogged on 2009 :(

I am trying to get useragent data with JS, to get which browser people are using. I then need to send those details to a MySQL table using Perl.

The added complication is that I need this to be done as the first page loads so I can't do it by sending vars in the query string, (can I).

the full webpage is made by mainpage.pl like this:

1. require header.pl ~ outputs the head section of the page including the JS for userAgent
2. require logo.pl ~ outputs the logo section of the page.

run rest of mainpage.pl

How should I send the UserAgent vars got in header.pl to one of the other perl scripts (where it can input it to the Db)?

Maybe I should be ding it some other way?

any pointers would be very helpful.
bazz

FishMonger
01-08-2009, 06:50 PM
If you need it when the page loads rather than a form submission, then you might need to look at using AJAX. Someone in the Javascript section should have more info in that direction.

oesxyl
01-08-2009, 07:23 PM
hi,

and happy new year everybody, :)

as FishMonger said you can use ajax. Something like that in javascript:


/* -*- c++ -*-
*/
function xhr() {};

xhr.prototype.create = function() {
var req = false;
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e2) {
try {
req = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e3) {
try {
req = new XMLHttpRequest();
} catch (e1) {
req = false;
}
}
}
return req;
}

function my_ajax_request(){
var x = new xhr();
var req = x.create();
req.open('POST','/mycgiscript.pl',true);
req.send('v1=6&v2=' + escape('sometext'));
}

// you could use this to get the data when the page is loaded
window.onload = my_ajax_request;

this will send v1 and v2 parameters with values 6 and sometext using post to the mycgiscript.pl.
In perl you will have the usual cgi processing of a post request.

best regards

bazz
01-08-2009, 09:22 PM
Thank you both.

I had hoped I could do this without relying on scripts which the user can switch off. However, I shall now embark on a journey into ajax :)

bazz

oesxyl
01-08-2009, 09:34 PM
Thank you both.

I had hoped I could do this without relying on scripts which the user can switch off. However, I shall now embark on a journey into ajax :)

bazz
it's not a big difference between what you already done with javascript and perl in few days you can work with it, :)

best regards