CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Saving variable result of a javascript being executed in Perl CGI

 



perlFun
User

Jun 11, 2015, 6:29 PM

Post #1 of 7 (2397 views)
Saving variable result of a javascript being executed in Perl CGI Can't Post

In my Perl CGI script, I have a constraint that one of the parameters passed to the script in the url will include a '#' char with information following it. I need this information. Unfortunately, '#' within a url designates an anchor, and everything beyond it is unaccessible by perl cgi. (for a description of why see this: http://www.perlmonks.org/?node_id=179484)

There is a javascript function (window.location.hash) which will retrieve the data in an URL anchor. Great! I can set perl cgi to deploy this javascript when the page loads as follows:

print $q->start_html(-onLoad=> "anchorVar =window.location.hash();");

Problem - I need to actually make use of this variable within the script. How do I access this variable ('anchorVar') outside this start_html function? Meaning, I'd like to be able to do something like "my $perlvar = anchorVar". Does it makes sense?

Thanks!


(This post was edited by perlFun on Jun 11, 2015, 6:32 PM)


Zhris
Enthusiast

Jun 11, 2015, 7:58 PM

Post #2 of 7 (2385 views)
Re: [perlFun] Saving variable result of a javascript being executed in Perl CGI [In reply to] Can't Post

Hi,


Quote
one of the parameters passed to the script in the url will include a '#' char


If you really are sending a # in the value of a query parameter then you need to make sure you escape it appropriately via URI::Escape. This is handled for you if you use the likes of URI::Query. Fundamentally, check out URI for a comprehensive solution to managing urls:


Code
use URI::Query; 

my $uri_query = URI::Query->new( key => 'foo#bar' );

print 'http://example.com/?', $uri_query->stringify; # http://example.com/?key=foo%23bar


If the # actually does designate a fragment then as stated in the perlmonks post, you cannot read the value of an anchor server side. Upon hitting http://example.com/#val and inspecting the request headers, the anchor is not sent at all:


Code
GET / HTTP/1.1 
Host: example.com


An anchor has a client side purpose, it was not designed to be processed server side. I suspect that this is an XY problem and the anchor data would be better suited in a query parameter:


Code
http://example.com/#val 
http://example.com/?key=val#val
http://example.com/?key=val


Could you describe your anchor data and the reason you need to read it server side.

Alternatively, there is the possibility that Ajax could be a suitable solution, but that depends on your cgis purpose. You would read the value of the anchor server side using window.location.hash, append this to your cgis query parameters, then send a request to your cgi.

Chris


(This post was edited by Zhris on Jun 11, 2015, 8:28 PM)


perlFun
User

Jun 11, 2015, 8:23 PM

Post #3 of 7 (2373 views)
Re: [Zhris] Saving variable result of a javascript being executed in Perl CGI [In reply to] Can't Post

Thanks a lot for the response.


Let me explain the reason I thought I had this constraint - perhaps there is a workaround.


This script essentially generates a table with a bunch of options for a user to fill out (there's some radio buttons, and a few form fields).

The initial call to the script is via another website. This website has URLs of the form: http://<site>/#/<info>/. In my script, I have a particular form field. This form MUST (company policy) have as its default value, the full URL of the website that initially called the script. Now, I could do this just send <info> as a standalone param and then do this:

my $baseURL = "http://<site>/#/";
my $default = $baseURL.$q->param(somevar);
print $q->start_form.
$q->textfield(
-name=> <my form>,
-size=>100,
-default=>$default).
$q->submit(-value=>"Submit").
$q->end_form);


The problem:
The user then has the option to alter that default value. If they alter it, they will always be altering it to an URL of that form http://<site>/#/<user's altered value>/. If they alter the default and then press submit, then it means the script will reload and be passing: http://<site>/#/<user's altered value>/, and I'm back with the same problem.


One work around: is there a way to pass only PART of what they've typed in a form upon pressing the submit button? I mean, when they alter it and press submit, is there a way to kind of grep out <user's altered value> and only pass that? If there's a way to do that, then my problem is solved.


(This post was edited by perlFun on Jun 11, 2015, 8:51 PM)


perlFun
User

Jun 11, 2015, 8:40 PM

Post #4 of 7 (2362 views)
Re: [perlFun] Saving variable result of a javascript being executed in Perl CGI [In reply to] Can't Post

Oh hey I think I have an idea. Can you let me know if I'm on the right track? I realized I can make use of the '-onsubmit' option when making the form. I can make a javascript function that parses urls of these form and only returns what comes after #:

my $baseURL = "http://<site>/#/";
my $default = $baseURL.$q->param(somevar);
print $q->start_form.
$q->textfield(
-name=> <my form>,
-size=>100,
-onsubmit => 'return somejavafunctiontoparsetheanchor(<user's altered value>)'
-default=>$default).
$q->submit(-value=>"Submit").
$q->end_form);


The question is, how do I pass <user's altered value> to this function?


Zhris
Enthusiast

Jun 11, 2015, 8:40 PM

Post #5 of 7 (2361 views)
Re: [perlFun] Saving variable result of a javascript being executed in Perl CGI [In reply to] Can't Post

Hi,

I was still editing my post when you replied. You need to look into escaping that # appropriately (see first part of my initial reply):


Code
http://<site>/#/ 
http://<site>/%23/



Quote
The question is, how do I pass <user's altered value> to this function


There are various ways, I would do something along the lines of http://stackoverflow.com/questions/11310218/how-access-form-fields-using-javascript.

Chris


(This post was edited by Zhris on Jun 11, 2015, 8:47 PM)


perlFun
User

Jun 11, 2015, 9:01 PM

Post #6 of 7 (2351 views)
Re: [Zhris] Saving variable result of a javascript being executed in Perl CGI [In reply to] Can't Post

Thanks a lot for the response. I looked at your edited post - I'd missed it originally. I have a question because I want to clarify my understanding. Are you indicating that URI::Query gives me a way to kind of redo the incoming URL? (meaning if the cgi url comes in as : http://<site>/#/<stuff> URI:Query could properly escape the incoming url to http://<site>/%23/<stuff>?


Zhris
Enthusiast

Jun 11, 2015, 9:19 PM

Post #7 of 7 (2348 views)
Re: [perlFun] Saving variable result of a javascript being executed in Perl CGI [In reply to] Can't Post

Hi,

Thats pretty much correct if you are referring to the url being passed in via the form, otherwise remember everything after the # of the actual incoming url will have been stripped off. Note that URI::Query should be used with query parameters, but the # that is of concern is actually in the path, therefore you should use URI::Escape directly. I would suggest using URI to parse the url and handle everything for you, but it would assume everything after the hash is a fragment and wouldn't recognize the # as being part of the path, however if you can safely assume fragments shouldn't occur then you could use a substitution beforehand:


Code
my $url = $q->param( 'url' ) // ''; 
$url =~ s/#/%23/g;
my $uri = URI->new( $url );
print $uri->as_string;


I don't fully understand your overall problem, but if an external website is calling your website via "http://<site>/#/<info>/", they need to escape that # in the path their end, or they are expecting you to handle it client side. Another suggestion is to hit a landing page which reads the fragment client side, escapes it, puts it in a query parameter i.e. ?val=..., then redirects to your cgi which retrieves it i.e. param('val'):

http://demo.massweb.co.uk/serversidefragment.pl#<stuff>

Code
use strict; 
use warnings FATAL => qw/all/;
use CGI::Carp qw/fatalsToBrowser/;
use CGI;
use Template;

my $cgi = CGI->new;

my $template = Template->new or die $Template::ERROR;

my $val = $cgi->param( 'val' );

my $placeholders = ( defined $val ) ? { view => 'main', val => $val } : { view => 'landing' } ;

print $cgi->header;

$template->process( \*DATA, $placeholders ) or die $template->error;

__DATA__
[%- IF view == 'landing' -%]
<script type="text/javascript">
var val = encodeURIComponent( window.location.hash );
var main_url = 'serversidefragment.pl?val=' + val;
window.location.href = main_url;
</script>
[%- ELSIF view == 'main' -%]
<html>
<head>
<title>...</title>
</head>
<body>
<p>[% val | html_entity %]</p>
</body>
</html>
[%- END -%]


Chris


(This post was edited by Zhris on Jun 11, 2015, 11:42 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives