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

Hello I have a funny (probably due to my lack of understanding of the inerds of Perl!) where I have set a global hash variable (using our), and then create some AJAX subroutines hash mappings to be exported to javascript (HTML page created via HTML::Template:Pro in the first instance). I have found that each "callback" does not persist the data to the global hash when printed in each subroutine.

Is there another way to maintain the returned values via AJAX for an HTML page with multi choice pre submission?

eg..(simple cut down for reference)

our %myhash; my $cgi = CGI->new(); my %funchash = ( 'SUBA' => \&suba, 'SUBB' => \&subb ); my $pjx = CGI::Ajax->new( %funchash ); my $var1="aaa"; $myhash{typea}=$var1; my $template="html/mypage.html"; my $tmpl= HTML::Template::Pro->new(filename => $template,loop_context_ +vars=>1,debug=>1,global_vars=>1); print $pjx->build_html( $cgi, $tmpl->output) sub suba { # this is invoked by user clicking on dropdown box A $myhash{typeb}="bbbb"; print "$myhash{typea}"; <- this is shown correctly print "$myhash{typeb}"; <- this is shown correctly } sub subb { # this is invoked after user clicked dropdown box B after A $myhash{typec}="cccc"; print "$myhash{typea}"; <- this is shown correctly print "$myhash{typeb}"; <- this is _NOT_ shown print "$myhash{typec}"; <- this is shown correctly }


Many Thanks
Gareth

Replies are listed 'Best First'.
Re: Global variables with CGI:Ajax
by Anonymous Monk on May 28, 2012 at 21:24 UTC

      Thanks for the info. I just did a simple print FILE test using $$ on the name and can see as you say it is a different process each time. This implies the entire screen on the browser is refreshed for each AJAX call right? I thought AJAX (in my simple mind) was clever in that it simply modified the element (div) on screen and used the called script/function to provide the modification?

      I am wondering why I both using AJAX in that case and just let the entire HTML page refresh with some hidden variables to control what actions occur on the next call of the cgi object.

      Any thoughts on that?

      Many Thanks

        This implies the entire screen on the browser is refreshed for each AJAX call right?

        Nope, it implies each HTTP request AJAX makes is serviced by a different CGI program instance.

        Read more about this (watch the video too) at learn about the internet,Web Programming: For Beginners, to get an overall picture of how the internet works, how tcp/ip, sockets, html, ajax, all fit together.