Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pass JavaScript variable to Perl subroutine and vice versa

Posted on 1998-12-03
3
Medium Priority
?
1,293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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'…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

721 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