poss has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I am trying to access a javascript variable from my perl code. All of this is in the same perl file. ex;

#!/home/vobadm/perl5/bin/perl -w<BR> <BR> use CGI qw/:standard/;<BR> <BR> my $q = CGI->new();<BR> <BR> use strict;<BR> use warnings;<BR> <BR> my $foo = "abc";<BR> <br><br> print "<script type='text/javascript'><BR> function changeText(str) {<BR> alert("FOO = " + str); // this equals "abc" <br> var bar = str;<BR> bar = "xyz";<BR> return bar; <BR> }<BR> </script>";<BR> print "<table align='center' border='10' >"; <BR> print "<td width='40%'><select id='buildSelection1' onchange='changeTe +xt(\"$foo\");'>"; <BR> <p>
At this point I would like to get the value of bar from my perl code
which now contains xyz
but I am not sure how to go about it.
Any help would be appreciated.

thanks

poss

Replies are listed 'Best First'.
Re: Access javascript variable from perl
by Corion (Patriarch) on Mar 11, 2015 at 20:37 UTC

    If you want to have data available for processing with Perl on the server, you will have to send it from the client ("browser") to the server.

    For example, you could set a field in a <FORM> tag to a value and then submit that form.

    Alternatively, one of the many Javascript libraries has ways of sending data using XMLHttpRequest to the server. For example with jQuery, there are the .get(), .post() and .html() methods that send data to the server and give the return value to Javascript code.