Solved

Pass JavaScript variable to Perl subroutine and vice versa

Posted on 1998-12-03
3
1,274 Views
Last Modified: 2009-10-22
Hi!
Is there a way to pass a JavaScript variable to a perl function? I need this variable to query the database and return the data to the JavaScript function.

This is a section of the code. I hope you can understand it!
ProductKey, FromRelease and ToRelease are selected variables from a form.

print "\n   if (ProductKey !=  form.key_name.options[0].selected) {";

print "\n     if (FromRelease != ToRelease) {";
print "\n       if (FromRelease < ToRelease) {";
               ?   $FromRelease = FromRelease;
               ?   $ToRelease = ToRelease;
               ?   $steps = &det_upg_steps($FromRelease,$ToRelease);
print "\n var step = \"$steps\";"; ?is this correct?
print "\n         display(FromRelease,ToRelease,step);";
 :
 :      

sub det_upg_steps {
  local($from_rel,$to_rel) = @_;
  local($steps);

  @res = &sql(q/SELECT DISTINCT STEP
                INTO :$steps
                FROM AD2000_UPG_STEPS
                WHERE AD2000_UPG_STEPS.FROM_REL = '$from_rel'
                AND AD2000_UPG_STEPS.TO_REL = '$to_rel'/);
  &sqlerr;

  return($steps);
}
 }";

0
Comment
Question by:bcoballe
3 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1275000
In order to communicate JavaScript => Perl you must run the perl script and pass the parameters to it. Here's one way of doing it, I guess there are a thousands of others.
script.pl?runFunction=det_upg_steps&var1=XXX&var2=XXX (a few lines of JavaScript to build the URL)

Then let your script check for runFunction, collect all variables passed as var1, var2, var3 etc and do eval on it. (a few lines of Perl)

Of course, if it's possible put det_upg_steps in its own file. Much easier...

Now, in order to get the result, there's a few solutions. About the ones that I come to think of this one seems to be the best:
Use a hidden frame. Print the output with JavaScript code so that it sets variables that will hold the result. Then the other (visible) frame reads the variables and use them. (a few lines of Perl and a few lines of JavaScript)

I don't think that should be so hard, but let me know if you need code in JavaScript or Perl.

Martin
0
 
LVL 1

Accepted Solution

by:
someu earned 100 total points
ID: 1275001
If you want to get the vars when he has clicked a form button, use:

<form method=get>
<input type=hidden value="" name="FormRelease"><!-- in name, is the name of the var you want to get-->
<input type=hidden value="" name="ToRelease">
<input type=submit value="submit form">
</form>

on cick the button the page will reload, but the values of the vars are in the search.
in the PERL code you must take the search vars with a famous PERL function.
If the search=="" then do not do anything.
:-D
0
 

Expert Comment

by:sumani100298
ID: 1275002
well, the way i do it is like this.

assuming you are having a FORM displayed and a BUTTON there to submit it
to the cgi program:

<script language="JavaScript">
<!-- hide from old browsers...
function TransferNSubmit(){
.
whatever calculations you want to do with the values in the hidden fields,
etc. you can do it here.
after all that..
...
document.mainform.submit();
}
//-->
</script>
<body>
<form name="mainform" action="yourPerlFileName.pl">


 all the values u display and get input from the user.....
<INPUT type="hidden" name="fromrelease" value="xxxxx">
<INPUT type="hidden" name="torelease" value="xxxxx">
<INPUT type="hidden" name="othervalue" value="xxxxx">
<INPUT type="button" name="SUBMIT" value="CLICK ME" onClick="TransferNSubmit();">
</form>

</body>


and in the perl file at the back end,
you can query for the values "fromrelease, torelease...." etc.
and if you do processing with fromrelease...etc at the perl side,
u can send back the values with the form, in the hidden field values..
$fromrelease = "something";
..
<input type="hidden" name="fromrelease" value="$fromrelease">
. something like this...

hth

mani
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question