in reply to Re^2: Using CGI::Ajax for multiple form buttons/divs simultaneously
in thread Using CGI::Ajax for multiple form buttons/divs simultaneously

Ok, taking just the first 2 points try this to see if I understand you correctly

#!/usr/bin/perl use strict; use CGI::Ajax; use CGI; my $cgi = new CGI(); my $pjx = new CGI::Ajax( 'function1' => \&myfunction1, 'function2' => \&myfunction2, # 'skip_header' => 1 ); print $pjx->build_html($cgi,\&main, {-charset=>'UTF-8', -expires=>'-1d'}); sub main { my $html = <<EOT; <HTML> <HEAD><title>CGI::Ajax Example</title></HEAD> <BODY> <div id="div2_text">Chapter = </div> BOOK <select id="select1" onchange="function1(['select1'],['div1_text'], +'POST'); "> <option value="">Select Book</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select></br> CHAPTER <div id="div1_text"></div> </BODY> </HTML> EOT return $html; } sub myfunction2 { return "Chapter = ".shift }; sub myfunction1 { my $book = shift; my @chap = (undef,['1A','1B','1C'], ['2A','2B','2C','2D'],['3A','3B']); my $option; for (@{$chap[$book]}){ $option .= qq!<option value="$_">$_</option>!; } return qq! <select id="select2" onchange="function2(['select2'],['div2_text'], +'POST'); "> $option </select>!; };
poj
  • Comment on Re^3: Using CGI::Ajax for multiple form buttons/divs simultaneously
  • Download Code

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

    I have noticed that every implementation of AJAX that I have laid eyes upon has one feature in common:

    print $pjx->build_html( $cgi, \&main, {-charset=>'UTF-8', -expires=>'- +1d'} );

    Is there some way of connecting the AJAX to more than one subroutine, rather than having only the "&main" option?

    Something more diversifiable may be what I need. For example, if I can succeed at having more than one option here, I could also incorporate the AJAX into the login side of the script which, for now, I have left without AJAX. Understanding this, in fact, may be key to helping me solve what I am doing now with multiple divs.

    I have been making more attempts to get AJAX working with multiple divs, but have yet to see success. Sometimes, "brute force" trial-and-error seems the only thing I can do, especially having never used the tool, in this case AJAX, before.

    Blessings,

    ~Polyglot~

      Is there some way of connecting the AJAX to more than one subroutine, rather than having only the "&main" option?
      Sure, that's no problem. When you initially create the CGI::Ajax instance, you can pass in as many "js_call_name => perl_coderef" pairs as you like. e.g.,
      my $pjx = CGI::Ajax->new(first => \&first_handler, second => \&second_ +handler); sub first_handler { ... } sub second_handler { ... }
      Note that you still call build_html in the exact same way - this is set up when the CGI::Ajax object is created, not when it's used.