Solved

Share variable between Javascript & PERL

Posted on 2002-03-16
11
260 Views
Last Modified: 2008-03-10
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
Comment
Question by:nascar_3
  • 6
  • 5
11 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6874015
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
 

Author Comment

by:nascar_3
ID: 6874096
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
 
LVL 16

Expert Comment

by:maneshr
ID: 6874547
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
 

Author Comment

by:nascar_3
ID: 6874594
Yes, that could provide me with some insight. I'd like to see what you would suggest.

Thanks!
0
 
LVL 16

Accepted Solution

by:
maneshr earned 200 total points
ID: 6874681
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:maneshr
ID: 6878234
nascar_3,

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

How did it work for you?

Let me know.
0
 

Author Comment

by:nascar_3
ID: 6878412
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
 

Author Comment

by:nascar_3
ID: 6884711
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
 

Author Comment

by:nascar_3
ID: 6884718
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
 
LVL 16

Expert Comment

by:maneshr
ID: 6884729
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
 

Author Comment

by:nascar_3
ID: 6884748
>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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

762 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

19 Experts available now in Live!

Get 1:1 Help Now