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

Hi
I am slightly confuse with the problem I am facing. Not really sure why, but confuse me a lot. Been seraching the web for anyone with some situation but no luck.let say the following is my code

use CGI::Ajax; use CGI; $pjx = new CGI::Ajax ('func','\&func'); $cgi = new CGI; $html=<<EOF; ... ... <Script> var previous_value; function user_click (current_value) { previous_value=current_value; func([],['change_location']); } </Script> <BODY> <a href="#" OnClick="user_click(somevalue)"> CLICK ONCE OK, SECOND TIME FAILED </a> <div id="change_location"></div> </BODY> </HTML> EOF sub func() { do something .. return something .. } $pjx->build_html($cgi,$html);


Ok, when it is open using browser, everything looks ok
when the link (href) is clicked for the first time, it works
but if it is clicked for the second time, it gave javascript error

At first I thought something wrong with ajax xml generated, but after further test, I figure out the script
previous_value=current_value;
is the cause of the error

As of now, I have workaround using hidden input, however, I wish to know why simple assignment is giving error

maybe somebody have the answer. Thank you

20080520 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: CGI::Ajax vs javascript global var
by varian (Chaplain) on Aug 24, 2007 at 19:28 UTC
    $pjx = new CGI::Ajax ('func','\&func');
    does not pass the Perl code reference properly, it should read:
    $pjx = new CGI::Ajax ('func' => \&func);
    otherwise the Perl call server side fails.

    You may want to have a look at one of the CGI::Ajax examples that come with the module.

      opps.. my mistake in the sample
      but in my code it has the correct syntax
      $pjx = new CGI::Ajax ('func' => \&func);

      coz it execute seccussfuly, on the first time but in second time, it complains about javascript "previous_value"

      my code is pretty long, so, I made a simplified version
      I tried it several time but ended up using workaround using
      hidden input to keep the previous value