...

View Full Version : javascript with perl cgi



lara123
03-17-2005, 11:45 AM
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

Philip M
03-17-2005, 06:38 PM
"All this should happen without the user getting to know."

Why?

lara123
03-18-2005, 04:22 AM
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.

glenngv
03-18-2005, 05:07 AM
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.

lara123
03-18-2005, 06:08 AM
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

glenngv
03-18-2005, 07:31 AM
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.

lara123
03-18-2005, 10:26 AM
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.

glenngv
03-18-2005, 11:05 AM
Ok, try this hidden iframe technique.

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


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

lara123
03-18-2005, 12:23 PM
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.

glenngv
03-21-2005, 05:04 AM
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.

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum