View Full Version : Possible to execute Perl script using onClick in link
Zyzyx
04-09-2009, 03:31 PM
Let's assume I have an HTML file.
<html>
<head>
</head>
<body>
<a onClick="../../cgi-bin/test/test.cgi" href="#">test</a>
</body>
</html>
And this is how my Perl/CGI script looks like:
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser) ;
use IO::Handle;
use locale;
use POSIX qw( locale_h );
setlocale( LC_CTYPE, 'iso_8859_1' );
STDOUT->autoflush();
STDERR->autoflush();
print <<HTML;
Content-type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en:us'>
<body>
test
</body>
</html>
HTML
system("mkdir test");
This is not working, but is it possible to create a directory using that script by clicking on that link in the HTML file? This is a test, because I want to do more advanced stuff.
KevinADC
04-09-2009, 05:41 PM
When you say its not working, what does that mean? What does it do? What does it not do? Perl has its own mkdir() function you should be using instead of trying to shell out to the operating system command. Many hosted sites will not allow you access to operating system commands, for obvious reasons, although on some you can.
oesxyl
04-09-2009, 06:28 PM
html is invalid both in your html page:
- no doctype
- empty head section
and in your perl script:
- namespace declaration for xhtml but doctype is html 4.0 strict
- no head section this time
best regards
Zyzyx
04-09-2009, 06:42 PM
When you say its not working, what does that mean? What does it do? What does it not do? Perl has its own mkdir() function you should be using instead of trying to shell out to the operating system command. Many hosted sites will not allow you access to operating system commands, for obvious reasons, although on some you can.
I am running my own Linux server.
The script works fine from the shell.
The HTML page also loads properly in the browser.
When I click on the link, nothing happens.
However, when I use this code, it gets directed to the script, but that's something I do not want.
onClick="self.location='../../cgi-bin/test/test.pl';"
I have a static page, where users can click on things. I want to save these clicks to a file, but the script should only be executed when I click on the link, but not displayed in the browser. I could use Javascript, but I am not really fluent in that...
shyam
04-09-2009, 07:23 PM
use hidden i/frames or ajax...even if u are not fluent there are several js libraries that help u do it easily...but, one word of caution that its not advisable to be creating directories on user clicks...who know how many times he may click :eek:
Zyzyx
04-09-2009, 07:29 PM
use hidden i/frames or ajax...even if u are not fluent there are several js libraries that help u do it easily...but, one word of caution that its not advisable to be creating directories on user clicks...who know how many times he may click :eek:
I was thinking of iframes too, but I have thousands of possible clicks on a page, which makes things not efficient.
AJAX is probably the way to go. Could you forward me to or recommend JS libraries? I will not be creating directories, just logging stuff. :)
shyam
04-09-2009, 07:33 PM
you can try prototype (http://prototypejs.org/) and jquery (http://jquery.com/) for starters
Shannon Blonk
04-09-2009, 08:14 PM
If you don't need to send information back to the client, AJAX and associated frameworks are overkill.
A couple options:
On your web page use the standard <sometag onclick="location='/some/script/you/want/called/but/not/displayed' "> or <a href='i/am/not/typing/all/that/again' > and in your click recording perl: print header( -status => '204 No response');
Browsers don't act upon No Response
Or useonclick="i=new Image();i.src='it/does/not/matter/what/image/if/any/you/send/because/we/are/not/inserting/it/into/the/document' "
and in your perl return 200, 204, or 302, or 4xx or whatever you like.
Zyzyx
04-09-2009, 08:20 PM
great, I will try this out. I am using HTTP to pass variables and store these using my script. Will this also work when I use that header?
Shannon Blonk
04-09-2009, 08:31 PM
Sure, you can use the query string to send as much to the server as you like, using javascript to build it.onclick="var query = 'something=' + someVariable + 'another=' + someFunctionReturn(); location='path/to/script.cgi?' + query; "
Again, for emphasis, the information flow with this technique is one way: browser -> server
Although, with the the image version, the client could determine error/OK ...
KevinADC
04-09-2009, 10:09 PM
Robots are going to have a field day with your website and click all those links and run your server into the ground. Or maybe thats what you want?
Zyzyx
04-09-2009, 11:27 PM
Robots are going to have a field day with your website and click all those links and run your server into the ground. Or maybe thats what you want?
It's a closed environment for people who I know...I am a scientist doing an online experiment. Thanks for the warning though. :)
It's a closed environment for people who I know...I am a scientist doing an online experiment. Thanks for the warning though. :)
It only takes one person to submit your site to a search engine for it to be seriously affected due to the present set up.
what do you want the link to do? it seems you do not want it to do anything other than create a directory? if that is so; you would be much better ~ I think ~ to use
1. a normal html href link to a perl script ( <a href='location_of_script'>link</a> )
2. where the perl script error checks and checks for an already existing dir
3. creates dir if necessary
4. skips if not necessary
5. redirects to your next html page
If you want the link to do something specific, you can still do so when following my suggestion above. the difference would be your redirect (at point 5), would send params to the next place (another script) for that function to be performed.
for the clicks you referred to, I would think it best to use JS to submit them to a js-built query string and when your link is clicked; not only would the directory be created but the 'clicked data' could be stored wherever you want it to be.
I am not sure if your requirement to click loads of things on the page will always be followed with a click of the link to perl file. if it will be then maybe checkboxes would be suitable?
my 2c
bazz
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.