• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Share variable between Javascript & PERL

What is the best way (if there is a way) to take a variable defined in a Javascript script> and pass it to the PERL code within the same CGI script?

This is all located in the same PERL script, and I'd like the variable 'thisuser' set by the cookie per the javascript and pass it on the the PERL below it, to use in determining the name of the file to open.


print "script language=javascript> \n";
print "//<!--\n";
print "if (document.cookie) { \n";
print "var thisuser=unescape(document.cookie); \n";
print "} \n";
print "document.write(thisuser) \n";
print "// -->\n";
print "</script> \n";

print "Your Saved Items: \n";
print "<select name=\"load_choices_A\" onchange=\"io([this.selectedIndex].value,'A')\"> \n";

$saved_file = "thisuser.save.txt";
open O,"<$saved_file" or die "can't read $saved_file because $!";
while( O> ){
   $thisname=(split/[=|]/)[1];
print "<OPTION value=$thisname>$thisname</option> \n";

}
close O;
print "</SELECT>\n";

I was thing perhaps setting an $ENV{THIS_USER}? But I can't find any good examples for syntax & structure, and all my experimentation has failed.

I know how when calling a different CGI with $ENV{QUERY_STRING}, but these are within the same script, and I do NOT want the page to reload. Is this even possible?

Thanks!
0
nascar_3
Asked:
nascar_3
  • 6
  • 5
1 Solution
 
maneshrCommented:
nascar_3,

"..What is the best way (if there is a way) to take a variable defined in a Javascript script> and pass  it to the PERL code within the same CGI script?..."

Can you pl. post the entire script here?

Also, pl explain how this script will be used? a URL where one can this script in action would be good.

i am trying to understand how the javascript is called and how the Perl part of the script is called.

This will help you get a more accurate answer, faster.
0
 
nascar_3Author Commented:
maneshr-

Well, I could, but its a REALLY large script, and everything else is not really relevant to my question. My script is working fine. The only thing you really don't see here is the function io(), but again that isn't a problem it works fine.

I just need to be able to get the 'thisuser' name from a cookie, and use that name to open up a previously saved text file on the server. The problem arises because I can only read the cookie with Javascript, and Can only Read/Write the server file with PERL. So all I need to understand is if it is possible for them to share a common variable within the same script, and not have to call a different script, or reload the current script to do it?

Anyway, my "work-in-progress" copy is here:
http://www.marqueemarket.com/cgi-bin/trainscape1.5.cgi
but it has a few rough edges to work out (this problem is one / its part of the login/out).
My current "live" copy is here:
http://www.marqueemarket.com/cgi-bin/trainscape.cgi
which doesn't use this code, it relies totally on cookies for the save feature, which has it's limitations.

This is a "learning" project for me, so please understand I'm probably doing lots of things the hard way right now, but as I'm learning PERL & JS, I'm trimming my code a little bit each day.
0
 
maneshrCommented:
nascar_3,

"..Well, I could, but its a REALLY large script, and everything else is not really relevant to my question. .."

Without the entire code, relevent or otherwise, it will be difficult to provide an accurate answer.

"..I just need to be able to get the 'thisuser' name from a cookie, and use that name to open up a previously
saved text file on the server. The problem arises because I can only read the cookie with Javascript,
and Can only Read/Write the server file with PERL. So all I need to understand is if it is possible
for them to share a common variable within the same script, and not have to call a different script,
or reload the current script to do it?..."

So how about if i write a small test script that will demonstrate how to read a cookie (set by JS in the same code) within Perl?

Let me know.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
nascar_3Author Commented:
Yes, that could provide me with some insight. I'd like to see what you would suggest.

Thanks!
0
 
maneshrCommented:
nascar_3,

".., that could provide me with some insight. I'd like to see what you would suggest..."

Excellent!!

Here is the script i was talking about. This is a self contained script that must be called by itself, from the browser.

NOTE: Remember to enable "Warn before accepting cookies" option of your browser, before trying this script.

Let me know how it goes.

=========nascar.pl
#!/usr/local/bin/perl

$|++;

print "Content-type: text/html\n\n";

$rm=$ENV{'REQUEST_METHOD'};     ##     GET or anything else

if ($rm=~ /^GET$/){     ##     GET!!

     ##     Show the HTML page with Javascript code for setting cookie.
     print qq{
     <SCRIPT LANGUAGE="JAVASCRIPT">
     <!--

     function Set_Cookie(name,value,expires,path,domain,secure) {
          var d = new Date((new Date()).getTime() + 302400000);
          document.cookie = name + "=" +escape(value) +
          ( (expires) ? ";expires=" + d.toGMTString() : "") +
          ( (path) ? ";path=" + path : "") +
          ( (domain) ? ";domain=" + domain : "") +
          ( (secure) ? ";secure" : "");
     }
     // -->
     </script>

     <BODY onLoad="Set_Cookie('username','nascar','expires');">
     };

     ##     ....also show a simple form with a submit button.
     ##     This form will call this same script.
     print qq{
     <FORM METHOD="POST" ACTION="$ENV{'SCRIPT_NAME'}">
     <B>Click =></B> &nbsp;<INPUT TYPE="SUBMIT">&nbsp; <B>to see the cookie details.</B>
     </FORM>
     };
}else{
     ##      Read the cookie.
     ($cookie_name,$cookie_value)=split(/=/,$ENV{'HTTP_COOKIE'});

     print qq{
     <B>Cookie Name = </B> $cookie_name<P>
     <B>Cookie Value= </B> $cookie_value<P>
     };
}
0
 
maneshrCommented:
nascar_3,

Did you get a chance to try the solution i posted above?

How did it work for you?

Let me know.
0
 
nascar_3Author Commented:
maneshr-

 I haven't gotten the opportunity to analize this yet. I put a copy on my server, and ran it, it worked OK, but haven't gotten the time to consider how it will work in my situation. I'll try to look at it deeper tonight when i get home.

Thanks!
0
 
nascar_3Author Commented:
maneshr-

 Sorry, I still haven't gotten around to examining this closely. I got side tracked onto something different, and this morning got hit with a virus that my step-son downloaded on one of those stupid mp3 file share programs.

 From the little bit I've thought about it, I believe this might give me some help when I get back around to this, but don't want to leave you hanging, and since no-one else has touched this, I'm going to accept your comment as an answer and award the points.

 If I have any other questions later, I might send you an email if thats OK.

 Thanks for your assistance!
0
 
nascar_3Author Commented:
maneshr-

 Sorry, I still haven't gotten around to examining this closely. I got side tracked onto something different, and this morning got hit with a virus that my step-son downloaded on one of those stupid mp3 file share programs.

 From the little bit I've thought about it, I believe this might give me some help when I get back around to this, but don't want to leave you hanging, and since no-one else has touched this, I'm going to accept your comment as an answer and award the points.

 If I have any other questions later, I might send you an email if thats OK.

 Thanks for your assistance!
0
 
maneshrCommented:
nascar_3,

"..got hit with a virus .."

Ouch!!! That must be painful. i remember the painful experience the only time i was hit with a virus & it was no fun either. Hopefully, your system will recover fast.

".. but don't want to leave you hanging, and since no-one else has touched this, I'm going to accept your comment as an answer and award the points..."

That is very kind of you indeed!!! i am certain that this solution will work for you. The only question would be to integrate it with your existing code.

"..If I have any other questions later, I might send you an email if thats OK."

if the question is directly related to the one posted here, please feel free to post a comment here & i will glad to assist.

Thanks.
0
 
nascar_3Author Commented:
>if the question is directly related to the one posted here, please feel free to post a comment here
& i will glad to assist.

Understood, & thanks again!


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

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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