Mercurial > boosterpack
view scripts/pjx_combo.pl @ 6:4246c7ef6487 boosterpack
[svn r8] bug fix -- holos need to be one int three compared to rares
author | robert |
---|---|
date | Tue, 08 Sep 2009 17:20:52 -0400 |
parents | 477258d09353 |
children |
line wrap: on
line source
1 #!C:/strawberry/perl/bin/perl.exe3 # CGI-Ajax: example script 'pjx_combo.pl'4 #5 # INSTALL: place in an apache location that can execute perl scripts6 #7 # this script demonstrates a set of dynamic select boxes, where the8 # selection in a box changes other select box contents, or html div9 # values. The data in each select box comes from the data anonymous10 # hash, but could just as easily come from a database connection, etc.11 #12 # N.B. this requires CGI__Ajax version >=0.4913 #14 # Also, this example has lots of stderr output, so follow your apache15 # log files to see what's going on.16 #17 use strict;18 use CGI::Ajax 0.49;19 use CGI;20 use vars qw( $data );22 # This is our data - top level keys get put in the leftmost select23 # box, next level of keys get the second select box. Values will end24 # up in the resultdiv html element25 $data = {26 'A' => { '1' => "A1", '2' => "A2", '3' => "A3", '42' => "A42" },27 'B' => { 'green' => "Bgreen", 'red' => "Bred" },28 'something' => { 'firefly' => "great show" },29 'final_thing' => { 'email' => "chunkeylover53", 'name' => "homer",30 'address' => "742 Evergreen Terrace" }31 };33 my $q = new CGI; # need a new CGI object35 # compose our list of functions to export to js36 my %hash = ( 'SetA' => \&set_listA,37 'SetB' => \&set_listB,38 'ShowResult' => \&show_result );40 my $pjx = CGI::Ajax->new( %hash ); # this is our CGI::Ajax object41 $pjx->js_encode_function('encodeURIComponent');43 $pjx->DEBUG(1); # turn on debugging44 $pjx->JSDEBUG(1); # turn on javascript debugging, which will place a45 # new div element at the bottom of our page showing46 # the asynchrously requested URL48 print $pjx->build_html( $q, \&Show_HTML ); # this builds our html49 # page, inserting js51 # This subroutine is responsible for outputting the HTML of the web52 # page. Note that I've added an additional javascript function to53 # erase/reset contents. This prevents strange effects from54 # overwriting a div without clearing it out first.55 sub Show_HTML {56 my $html = <<EOT;57 <HTML>58 <HEAD><title>Combo Example</title>59 <SCRIPT>61 // define some reset functions to properly clear out the divs62 function resetdiv( ) {63 if ( arguments.length ) {64 // reset a specific div65 for(var i = 0; i < arguments.length; i++ ) {66 document.getElementById(arguments[i]).innerHTML = "";67 }68 } else {69 // just reset all the divs70 document.getElementById("listAdiv").innerHTML = "";71 document.getElementById("listBdiv").innerHTML = "";72 document.getElementById("resultdiv").innerHTML = "";73 }74 }76 </SCRIPT>78 </HEAD>79 <BODY onload="resetdiv(); SetA([],['listAdiv']); return true;" >80 <form>81 <div id="listAdiv"></div>82 <div id="listBdiv"></div>83 <div id="resultdiv" style="border: 1px solid black; width: 240px; height: 80px; overflow: auto">84 </div>85 <input type="text" name="textfield">86 <input type="submit" name="Submit" value="Submit"88 </form>89 </BODY>90 </HTML>91 EOT93 return($html);94 }96 # these are my exported functions - note that set_listA and set_listB97 # are just returning html to be inserted into their respective div98 # elements.99 sub set_listA {100 # this is the returned text... html to be displayed in the div101 # defined in the javascript call102 my $txt = qq!<select id="listA" name="listA_name" size=3!;103 $txt .= qq! onclick="resetdiv('resultdiv'); SetB( ['listA'], ['listBdiv'] ); return true;">!;104 # get values from $data, could also be a db lookup105 foreach my $topval ( keys %$data ) {106 $txt .= '<option value='. $topval . '>'. $topval .' </option>';107 }108 $txt .= "</select>";109 print STDERR "set_listA:\n";110 print STDERR "returning $txt\n";111 return($txt);112 }114 sub set_listB {115 my $listA_selection = shift;116 print STDERR "set_listB: received $listA_selection .\n";118 # this is the returned text... html to be displayed in the div119 # defined in the javascript call120 my $txt = qq!<select multiple id="listB" name="listB_name" size=3 style="width: 140px"!;121 $txt .= qq! onclick="ShowResult( ['listA','listB'], ['resultdiv'] ); return true;">!;123 # get values from $data, could also be a db lookup124 foreach my $midval ( keys %{ $data->{ $listA_selection } } ) {125 $txt .= '<option value=' . $midval . '>' . $midval . "</option>";126 }127 $txt .= "</select>";128 print STDERR "set_listB:\n";129 print STDERR "returning $txt\n";130 return($txt);131 }133 sub show_result {134 my $listA_selection = shift;135 my $txt = "";136 # this loop is needed in case the user selected multiple elements in137 # the second select box, listB138 while ( @_ ) {139 my $in = shift;140 $txt .= $data->{ $listA_selection }->{ $in } . "<br>";141 }143 print STDERR "show_result - returning txt with value: $txt\n";144 return( $txt );145 }