Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1322
  • Last Modified:

Pass JavaScript variable to Perl subroutine and vice versa

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
bcoballe
Asked:
bcoballe
1 Solution
 
martinagCommented:
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
 
someuCommented:
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
 
sumani100298Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now