Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript with perl cgi

    Hi,
    i have a perl cgi form with an "onClick" button. When the user clicks that button, i basically need to run another perl script that runs a command and stores the information in a file. All this should happen without the user getting to know.
    I do this by calling a javascript function when the button is clicked, which in turn calls the perl-cgi script. (all i do is open a new window and pass in the paramateres to it -- there surely would be a better way??) but i need the same form to display pass or fail results. Could someone please explain how to return a value from a perl-cgi script to the javascript function, which in turn can post the result on the same form.
    the form looks something like this

    script_name | systen name | Run_button | Result

    so when the user clicks the run_button, i need to remotely execute the script on system and display pass fail.

    i know how to run the script etc, but i need to figure out a way, to do this on the same form.

    Thanks,
    lara
    Last edited by lara123; 03-17-2005 at 11:48 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    "All this should happen without the user getting to know."

    Why?

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, by saying that i meant the original form should still appear on the screen.
    Could someone please expplain how to return a value from a perl script to a javascript??
    My javascript function needs to call a perl script and that perl script should return a value to the javascript.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,964
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by lara123
    the form looks something like this

    script_name | systen name | Run_button | Result
    Why not just let the perl script generate the form again with the result. That way, you won't have to bother with cross-browser issues of calling the perl script via javascript. With that very simple form, I feel that it's overkill to use XMLHTTP or iframe technique just to call the perl script without reloading the page.

  • #5
    New Coder
    Join Date
    Mar 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the response, but i think i need to get a few basic questions clear first.
    On the form i have multiple Run_buttons, which are not submit buttons, but are OnClick buttons. When the user clicks a OnClick button, as far as i know i need to call a javascript function. Can i call a perl function instead ???
    If not, i think i come back to my original problem. Could you ploease clarify this onClick button functionality.
    Regards,
    lara

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,964
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Why don't you use submit buttons?

    <form name="perlForm" action="run.pl" method="post">
    script_name | system name |
    <input type="submit" name="btnSubmit" value="Run" /> |
    </form>

    When the submit button is clicked, it goes to the form action specified which is run.pl in this case. After processing, the perl script should generate the form again.

  • #7
    New Coder
    Join Date
    Mar 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, i belive u missed the point.
    my form, to be more explicit looks like this:

    SCRIPT_NAMES | SYSTEM_NAMES | RUN_Button | RESULT
    script1_____| system1_______|run_button__| pass/fail
    script2_____| system2________run_button__ |pass/fail

    .....

    now when i click on the run button i need to execute the code "run.pl" and display the result under results column. if i use submit button, i again open a new window and how do i regenarate the form. I don't care what button i use, so long as i can on clicking that button, execute some perl code and post the result on the same form. Any further suggestins are welcome.
    Last edited by lara123; 03-18-2005 at 10:35 AM.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,964
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Ok, try this hidden iframe technique.
    Code:
    <html>
    <head>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    function callback(result, suffix){
      document.getElementById("result"+suffix).innerHTML = result;
    }
    </script>
    <form name="perlForm" action="run.pl" method="post">
    
    script_name1 | system name1 | <input type="submit" name="btnSubmit1" value="Run" onclick="this.form.target='ifr1'" /> | <span id="result1"></span>
    script_name2 | system name2 | <input type="submit" name="btnSubmit2" value="Run" onclick="this.form.target='ifr2'" /> | <span id="result2"></span>
    script_name3 | system name3 | <input type="submit" name="btnSubmit3" value="Run" onclick="this.form.target='ifr3'" /> | <span id="result3"></span>
    ...
    
    <iframe name="ifr1" id="ifr1" src="about:blank" style="display:none; width:0px; height:0px"></iframe>
    <iframe name="ifr2" id="ifr2" src="about:blank" style="display:none; width:0px; height:0px"></iframe>
    <iframe name="ifr3" id="ifr3" src="about:blank" style="display:none; width:0px; height:0px"></iframe>
    ...
    </form>
    </body>
    </html>
    Then in run.pl, check which button is clicked by checking which button name is submitted. Only the clicked submit button is sent to the server, so only one will have a value. Extract the index and then execute the corresponding perl code. After processing the script, the perl script must generate this HTML output that calls the callback function passing the results.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    window.onload = function(){
      if (parent.callback) parent.callback("Pass", 1); //use the values processed by the perl script.
    }
    </script>
    </head>
    <body>
    </body>
    </html>

  • #9
    New Coder
    Join Date
    Mar 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for helping, but i dont quite understand this technique:

    what is this run.pl script doing?? could you explain the window.onload = function() {..} part please. It does not seem to work the way it is. And i don't understand how it should work in the first place.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,964
    Thanks
    0
    Thanked 236 Times in 233 Posts
    The run.pl is your perl script. Of course, you can change it to whatever your perl script name is.

    This is how the technique works.

    When a submit button is clicked, the form is submitted to the corresponding hidden iframe. So the perl script specified in the form action is executed. In the perl script, you can now detect which submit button is clicked and then execute the corresponding code for the corresponding system name. Once the processing is done and the generated HTML is rendered to the iframe, the onload event is triggered calling the callback function and passing the result of the processing. The callback function is executed and the result is posted in the corresponding row. This happens in the background and the page does not reload. To make the user see something while the perl script is running, you can put a status message in the result column.
    Code:
    <html>
    <head>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    function callback(result, suffix){
      document.getElementById("result"+suffix).innerHTML = result;
    }
    
    function setTarget(f, suffix){
      f.target = 'ifr'+suffix;
      document.getElementById("result"+suffix).innerHTML = "Processing..."
    }
    </script>
    <form name="perlForm" action="run.pl" method="post">
    
    script_name1 | system name1 | <input type="submit" name="btnSubmit1" value="Run" onclick="setTarget(this.form, 1)" /> | <span id="result1"></span>
    script_name2 | system name2 | <input type="submit" name="btnSubmit2" value="Run" onclick="setTarget(this.form, 2)" /> | <span id="result2"></span>
    script_name3 | system name3 | <input type="submit" name="btnSubmit3" value="Run" onclick="setTarget(this.form, 3)" /> | <span id="result3"></span>
    ...
    
    <iframe name="ifr1" id="ifr1" src="about:blank" style="display:none; width:0px; height:0px"></iframe>
    <iframe name="ifr2" id="ifr2" src="about:blank" style="display:none; width:0px; height:0px"></iframe>
    <iframe name="ifr3" id="ifr3" src="about:blank" style="display:none; width:0px; height:0px"></iframe>
    ...
    </form>
    </body>
    </html>


  •  

    Posting Permissions

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