www.webdeveloper.com
Results 1 to 5 of 5

Thread: Execute Perl script in JavaScript

  1. #1
    Join Date
    Oct 2007
    Posts
    7

    Execute Perl script in JavaScript

    I'm new to JavaScript and need coding help to execute a Perl script in the background in JavaScript.
    What I need is:
    1. JavaScript to pass some parameters to a Perl script on Linux server.
    2. Perl script executes and returns some text back to the JavaScript.

    How can I do this? Here's some snippets of the JavaScript that I'm coding now. Appreciate any help on this. Thanks.


    <html>
    <head>
    <script type="text/javascript">
    function Run_Perl() {
    // Code to run the Perl script (eg. "test.pl")
    // Input parameter : OrderNum.value (Text entered by user in the OrderNum Text field)
    var results; // A variable containing the text returned by "test.pl"
    }

    </script>
    </head>

    <body>
    <p>
    <form id="ThisForm" action="another_script.cgi" method=post>
    <pre>
    <input type="text" name="OrderNum">
    <input type="button" name="RunPerl" value="RUN PERL" onclick="Run_Perl()">
    </pre>
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    You'll have to use AJAX, or XMLHttpRequest as it's sometimes called. Do a google search for AJAX, or mootools or Prototype. Mootools and Prototype, and jQuery for that matter, are JavaScript libraries that have AJAX built into them.

  3. #3
    Join Date
    Oct 2007
    Posts
    7
    Thanks a lot for your advice. AJAX is great! I've tried it today and it's exactly what I need.

    I still have some problems though... Is it possible to make AJAX call a Perl script? I didn't know how to do it, so I made a PHP script instead, and let AJAX call it. I tried to add some code in the PHP script to execute the Perl script, but wasn't successful. Do you have any idea on this? Appreciate your advice and some sample code, if possible. Thanks!

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    You can use Perl with AJAX. Just replace the path to the PHP file with the path to the Perl script. You'll still have access to the $_POST or $_GET variables, depending on which method you are using for the AJAX request. Also, the JavaScript function that generates the AJAX request must come from the same domain as the target of the AJAX request. You can't have a JavaScript hosted on www.site-a.com make an AJAX request to www.site-b.com. If www.site-a.com imports a JavaScript file from www.site-b.com, and that JavaScript makes an AJAX call to www.site-b.com, then you can make an AJAX call "from" www.site-a.com to www.site-b.com. This is how Google Maps work.

  5. #5
    Join Date
    Oct 2007
    Posts
    7
    Thanks! I've created a CGI Perl script and make AJAX call it. My webpage works now. Thanks again for your great advice!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles