Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Using CGI::Ajax for multiple form buttons/divs simultaneously

by dsheroh (Monsignor)
on Mar 19, 2018 at 12:03 UTC ( [id://1211235]=note: print w/replies, xml ) Need Help??


in reply to Using CGI::Ajax for multiple form buttons/divs simultaneously

It's not entirely clear what you're asking, but I think you want to make a single AJAX call to the Perl back end, then use the (single) response to populate multiple divs on the displayed page. Is that correct?

If so, this is covered by the second usage example in the CGI::Ajax documentation. Just return a list of multiple values from the Perl handler sub (return 'first', 'second';), specify a list of divs on the HTML side (onclick="processFormInputs(['next_verse'],['first_div', 'second_div'], 'POST');), and you should be set.

If you need something more specialized than that, the third usage example shows the syntax to tell CGI::Ajax to pass the returned value(s) off to an arbitrary javascript function, where you can fold, spindle, and mutilate the data to your heart's content, then assign the pieces to whatever divs you like, including a basic example of a javascript function which might be used to do this.

(Note that it's been several years since I last used CGI::Ajax, so I could be misremembering some of the details, but the documentation matches up with what I think I remember, so it's probably at least close to correct.)

Replies are listed 'Best First'.
Re^2: Using CGI::Ajax for multiple form buttons/divs simultaneously
by Polyglot (Chaplain) on Mar 20, 2018 at 12:04 UTC

    I've made significant progress in the last hour, and I just wanted to thank you for giving me one of the most important clues so far--that of returning multiple values from the subroutine. I am still struggling, however, with the AJAX apparently not reading all of the form variables upon callback to the Perl script, which means I am not able to access the correct data to put into the div. The div is being updated with the correct translation/version, but it is doing so with the default record number (1) instead of the currently selected record from the menu (a separate div, one which is not invoked with the comparison div).

    I have been attempting to pass this information via a hidden form field, but it seems to be lost in transit using AJAX on an unrelated div. Am I expecting the wrong thing?

    Blessings,

    ~Polyglot~

Re^2: Using CGI::Ajax for multiple form buttons/divs simultaneously
by Polyglot (Chaplain) on Mar 20, 2018 at 10:19 UTC

    I'm discovering that it's more complicated for me than perhaps for some others on account of UTF8 encoding issues. I need to decode text coming in from the form, but with the AJAX apparently connecting to just the specified function/subroutine from the perl script, even the initial "my/our" variable declarations seem to be bypassed on the callback, and the code ends up trying to decode the UTF8 characters twice--which either results in an error message, or (without decoding), text that is incorrectly displayed. I wish I understood exactly what the AJAX is doing and/or needing to do in terms of following the code.

    I still seem to be no further along than I was at the start. However, I may have to run some experiments on text using pure English….so much for being a polyglot!

    Blessings,

    ~Polyglot~

      Yeah, that is the one major annoyance with CGI::Ajax, it kind of takes over completely. When you call build_html, it actually spits out a big mess of javascript into your page to make its magic work, and then, when you call one of its javascript helper functions, it intercepts that and takes over the back-end request handling, bypassing whatever your normal dispatch method might be. Towards the end of my time using CGI::Ajax, I delved into that and managed to at least partially unravel it and figure out how to bypass some of the magic, but I don't recall any of the details, only that I did it.

      These days, I'm using Dancer and Dancer::Plugin::Ajax, which requires me to manually write a little bit of javascript to invoke the ajax functionality, but it's much more transparent and fits into the rest of the application's framework, so I feel it ends up being a lot easier to work with overall. But that might not be an option for you if you're already dealing with legacy CGI scripts and aren't in a position to migrate to Dancer.

        To help me remember this myself in the future, should I seek to utilize CGI::Ajax again, I wish to make the following response to what you have mentioned above.

        Everything must be executed from within the &main subroutine. Anything else will fall outside of the purview of Ajax upon callback from the client.

        I had organized my code, as I usually do, by taking care of preliminaries before running the main subroutine--such as collecting my form inputs and setting up default values to certain variables. But instead of "compartmentalizing" things like this, and running them in their necessary A-B-C order, with Ajax, I have learned I must "nest" everything into the main subroutine.

        It seems someone could update the Ajax explanations, which I think I have read several times without realizing this fact, with a little more clarity. The TIMTOWTDI philosophy seems rather narrowed by the use of CGI::Ajax, for the code must be structured in just the right way to make it work.

        As for the "big mess of javascript," I have found CGI::Ajax far less unwieldy than jQuery and similar options which seem more popular. I'm happy with the small amount of JavaScript that the Ajax adds to the page.

        Blessings,

        ~Polyglot~

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found