Mercurial > rlmcintyre
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/BoosterPack/scripts/.svn/text-base/pjx_dynselect.pl.svn-base Mon Sep 27 16:57:26 2010 -0400 1.3 @@ -0,0 +1,130 @@ 1.4 +#!C:/strawberry/perl/bin/perl.exe 1.5 + 1.6 +# dynamic select boxes, using a db 1.7 + 1.8 +use strict; 1.9 +use CGI::Ajax; 1.10 +use CGI; 1.11 +use DBI; 1.12 + 1.13 +my $q = new CGI; 1.14 + 1.15 +### phone book database 1.16 +# CREATE TABLE `phonebook` ( 1.17 +# `login` varchar(10) NOT NULL, 1.18 +# `fullname` varchar(200) NOT NULL, 1.19 +# `areacode` int(10) unsigned NOT NULL default '123', 1.20 +# `phone` varchar(7) NOT NULL 1.21 +# ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Users and phone numbers'; 1.22 +# 1.23 +my $exported_fx = sub { 1.24 + my $searchterm = shift; 1.25 + my $sql = qq< select login from phonebook where login like ? or fullname like ? >; 1.26 + my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss'); 1.27 + my $sth = $dbh->prepare( $sql ); 1.28 + $sth->execute( $searchterm . '%', $searchterm . '%' ); 1.29 + 1.30 + # start off the div contents with select init 1.31 + my $html = qq!<select name="users" id="users" style="width:440px;" 1.32 + onClick="details( ['users'],['ddiv'] ); return true;">\n!; 1.33 + 1.34 + 1.35 + my $firstrow = $sth->fetch(); 1.36 + if ( defined $firstrow ) { 1.37 + $html .= qq!<option selected>! . $firstrow->[0] . qq!</option>\n!; 1.38 + 1.39 + # dot on each option from the db 1.40 + while ( my $row = $sth->fetch() ) { 1.41 + # $row->[0] will contain the login name 1.42 + $html .= qq!<option>! . $row->[0] . qq!</option>\n!; 1.43 + } 1.44 + 1.45 + } 1.46 + # close off the select and return 1.47 + $html .= qq!</select>\n!; 1.48 + 1.49 + return($html); 1.50 +}; 1.51 + 1.52 +my $get_details = sub { 1.53 + my $login = shift; 1.54 + my $sql = qq< select * from phonebook where login = ? >; 1.55 + my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss'); 1.56 + my $sth = $dbh->prepare( $sql ); 1.57 + $sth->execute( $login ); 1.58 + 1.59 + my $html = ""; 1.60 + 1.61 + my $row = $sth->fetch(); 1.62 + if ( defined $row ) { 1.63 + $html .= "Login: " . $row->[0] . "<br>"; 1.64 + $html .= "Full Name: " . $row->[1] . "<br>"; 1.65 + $html .= "Area Code: " . $row->[2] . "<br>"; 1.66 + $html .= "Phone: " . $row->[3] . "<br>"; 1.67 + } else { 1.68 + $html .= "<b>No Such User $login</b>\n"; 1.69 + } 1.70 + return($html); 1.71 +}; 1.72 + 1.73 + 1.74 +my $Show_Form = sub { 1.75 + my $html = ""; 1.76 + $html .= <<EOT; 1.77 +<HTML> 1.78 +<HEAD><title>CGI::Ajax Example</title> 1.79 +</HEAD> 1.80 +<BODY> 1.81 + Who are you searching for?<br> 1.82 + Start typing and matches will display in the select box.<br> 1.83 + Selecting a match will give you details. 1.84 + <br> 1.85 + <form> 1.86 + <input type="text" name="searchterm" id="searchterm" size="16" 1.87 + onkeyup="search( ['searchterm'], ['rdiv'] ); return true;"><br> 1.88 + 1.89 +EOT 1.90 + 1.91 + $html .= dump_table(); 1.92 + 1.93 + $html .= <<EOT; 1.94 + <div id="rdiv" style="border: 1px solid black; width: 440px; 1.95 + height: 80px; overflow: auto"></div> 1.96 + <br> 1.97 + <div id="ddiv" style="border: 1px solid black; width: 440px; 1.98 + height: 80px; overflow: auto"></div> 1.99 + 1.100 + <br><a href="pjx_dynselect.txt">Show Source</a><br> 1.101 + </form> 1.102 +</BODY> 1.103 +</HTML> 1.104 +EOT 1.105 + return $html; 1.106 +}; 1.107 + 1.108 +sub dump_table { 1.109 + my $sql = qq< select login from phonebook >; 1.110 + my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss'); 1.111 + my $sth = $dbh->prepare( $sql ); 1.112 + $sth->execute(); 1.113 + 1.114 + my $html = "<table><tr><th>Current Logins in DB</th></tr>"; 1.115 + 1.116 + while ( my $row = $sth->fetch() ) { 1.117 + $html .= "<tr><td>" . $row->[0] . "</td></tr>"; 1.118 + } 1.119 + 1.120 + $html .= "</table>"; 1.121 + return($html); 1.122 +} 1.123 + 1.124 +my $pjx = CGI::Ajax->new( 1.125 + search => $exported_fx, 1.126 + details => $get_details 1.127 + ); 1.128 +$pjx->JSDEBUG(1); 1.129 +$pjx->DEBUG(1); 1.130 + 1.131 +# not show the html, which will include the embedded javascript code 1.132 +# to handle the ajax interaction 1.133 +print $pjx->build_html($q,$Show_Form); # this outputs the html for the page