Solved

Pass JavaScript variable to Perl subroutine and vice versa

Posted on 1998-12-03
3
1,262 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now