annotate BoosterPack/scripts/.svn/text-base/pjx_dynselect.pl.svn-base @ 0:0d795f02a8bb tip

initial committ. what was I thinking?
author Robert McIntyre <rlm@mit.edu>
date Mon, 27 Sep 2010 16:57:26 -0400
parents
children
rev   line source
rlm@0 1 #!C:/strawberry/perl/bin/perl.exe
rlm@0 2
rlm@0 3 # dynamic select boxes, using a db
rlm@0 4
rlm@0 5 use strict;
rlm@0 6 use CGI::Ajax;
rlm@0 7 use CGI;
rlm@0 8 use DBI;
rlm@0 9
rlm@0 10 my $q = new CGI;
rlm@0 11
rlm@0 12 ### phone book database
rlm@0 13 # CREATE TABLE `phonebook` (
rlm@0 14 # `login` varchar(10) NOT NULL,
rlm@0 15 # `fullname` varchar(200) NOT NULL,
rlm@0 16 # `areacode` int(10) unsigned NOT NULL default '123',
rlm@0 17 # `phone` varchar(7) NOT NULL
rlm@0 18 # ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Users and phone numbers';
rlm@0 19 #
rlm@0 20 my $exported_fx = sub {
rlm@0 21 my $searchterm = shift;
rlm@0 22 my $sql = qq< select login from phonebook where login like ? or fullname like ? >;
rlm@0 23 my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss');
rlm@0 24 my $sth = $dbh->prepare( $sql );
rlm@0 25 $sth->execute( $searchterm . '%', $searchterm . '%' );
rlm@0 26
rlm@0 27 # start off the div contents with select init
rlm@0 28 my $html = qq!<select name="users" id="users" style="width:440px;"
rlm@0 29 onClick="details( ['users'],['ddiv'] ); return true;">\n!;
rlm@0 30
rlm@0 31
rlm@0 32 my $firstrow = $sth->fetch();
rlm@0 33 if ( defined $firstrow ) {
rlm@0 34 $html .= qq!<option selected>! . $firstrow->[0] . qq!</option>\n!;
rlm@0 35
rlm@0 36 # dot on each option from the db
rlm@0 37 while ( my $row = $sth->fetch() ) {
rlm@0 38 # $row->[0] will contain the login name
rlm@0 39 $html .= qq!<option>! . $row->[0] . qq!</option>\n!;
rlm@0 40 }
rlm@0 41
rlm@0 42 }
rlm@0 43 # close off the select and return
rlm@0 44 $html .= qq!</select>\n!;
rlm@0 45
rlm@0 46 return($html);
rlm@0 47 };
rlm@0 48
rlm@0 49 my $get_details = sub {
rlm@0 50 my $login = shift;
rlm@0 51 my $sql = qq< select * from phonebook where login = ? >;
rlm@0 52 my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss');
rlm@0 53 my $sth = $dbh->prepare( $sql );
rlm@0 54 $sth->execute( $login );
rlm@0 55
rlm@0 56 my $html = "";
rlm@0 57
rlm@0 58 my $row = $sth->fetch();
rlm@0 59 if ( defined $row ) {
rlm@0 60 $html .= "Login: " . $row->[0] . "<br>";
rlm@0 61 $html .= "Full Name: " . $row->[1] . "<br>";
rlm@0 62 $html .= "Area Code: " . $row->[2] . "<br>";
rlm@0 63 $html .= "Phone: " . $row->[3] . "<br>";
rlm@0 64 } else {
rlm@0 65 $html .= "<b>No Such User $login</b>\n";
rlm@0 66 }
rlm@0 67 return($html);
rlm@0 68 };
rlm@0 69
rlm@0 70
rlm@0 71 my $Show_Form = sub {
rlm@0 72 my $html = "";
rlm@0 73 $html .= <<EOT;
rlm@0 74 <HTML>
rlm@0 75 <HEAD><title>CGI::Ajax Example</title>
rlm@0 76 </HEAD>
rlm@0 77 <BODY>
rlm@0 78 Who are you searching for?<br>
rlm@0 79 Start typing and matches will display in the select box.<br>
rlm@0 80 Selecting a match will give you details.&nbsp;
rlm@0 81 <br>
rlm@0 82 <form>
rlm@0 83 <input type="text" name="searchterm" id="searchterm" size="16"
rlm@0 84 onkeyup="search( ['searchterm'], ['rdiv'] ); return true;"><br>
rlm@0 85
rlm@0 86 EOT
rlm@0 87
rlm@0 88 $html .= dump_table();
rlm@0 89
rlm@0 90 $html .= <<EOT;
rlm@0 91 <div id="rdiv" style="border: 1px solid black; width: 440px;
rlm@0 92 height: 80px; overflow: auto"></div>
rlm@0 93 <br>
rlm@0 94 <div id="ddiv" style="border: 1px solid black; width: 440px;
rlm@0 95 height: 80px; overflow: auto"></div>
rlm@0 96
rlm@0 97 <br><a href="pjx_dynselect.txt">Show Source</a><br>
rlm@0 98 </form>
rlm@0 99 </BODY>
rlm@0 100 </HTML>
rlm@0 101 EOT
rlm@0 102 return $html;
rlm@0 103 };
rlm@0 104
rlm@0 105 sub dump_table {
rlm@0 106 my $sql = qq< select login from phonebook >;
rlm@0 107 my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss');
rlm@0 108 my $sth = $dbh->prepare( $sql );
rlm@0 109 $sth->execute();
rlm@0 110
rlm@0 111 my $html = "<table><tr><th>Current Logins in DB</th></tr>";
rlm@0 112
rlm@0 113 while ( my $row = $sth->fetch() ) {
rlm@0 114 $html .= "<tr><td>" . $row->[0] . "</td></tr>";
rlm@0 115 }
rlm@0 116
rlm@0 117 $html .= "</table>";
rlm@0 118 return($html);
rlm@0 119 }
rlm@0 120
rlm@0 121 my $pjx = CGI::Ajax->new(
rlm@0 122 search => $exported_fx,
rlm@0 123 details => $get_details
rlm@0 124 );
rlm@0 125 $pjx->JSDEBUG(1);
rlm@0 126 $pjx->DEBUG(1);
rlm@0 127
rlm@0 128 # not show the html, which will include the embedded javascript code
rlm@0 129 # to handle the ajax interaction
rlm@0 130 print $pjx->build_html($q,$Show_Form); # this outputs the html for the page