Web Analytics Made Easy -
StatCounter Possible to execute Perl script using onClick in link - CodingForum

Announcement

Collapse
No announcement yet.

Possible to execute Perl script using onClick in link

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Possible to execute Perl script using onClick in link

    Let's assume I have an HTML file.

    Code:
    <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:

    Code:
    #!/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.

  • #2
    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.

    Comment


    • #3
      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

      Comment


      • #4
        Originally posted by KevinADC View Post
        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.

        Code:
        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...

        Comment


        • #5
          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
          You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

          Comment


          • #6
            Originally posted by shyam View Post
            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
            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.

            Comment


            • #7
              you can try prototype and jquery for starters
              You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

              Comment


              • #8
                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
                Code:
                <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:
                Code:
                print header( -status => '204 No response');
                Browsers don't act upon No Response

                Or use
                Code:
                onclick="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.

                Comment


                • #9
                  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?

                  Comment


                  • #10
                    Sure, you can use the query string to send as much to the server as you like, using javascript to build it.
                    Code:
                    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 ...

                    Comment


                    • #11
                      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?

                      Comment


                      • #12
                        Originally posted by KevinADC View Post
                        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.

                        Comment


                        • #13
                          Originally posted by Zyzyx View Post
                          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.

                          Edit:
                          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
                          Last edited by bazz; Apr 9, 2009, 07:11 PM.
                          "The day you stop learning is the day you become obsolete"! - my late Dad.

                          Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
                          Useful MySQL resource
                          Useful MySQL link

                          Comment

                          Working...
                          X