annotate scripts/pjx_dynselect.pl @ 0:477258d09353 boosterpack

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