rlm@0: #!C:/strawberry/perl/bin/perl.exe rlm@0: rlm@0: # dynamic select boxes, using a db rlm@0: rlm@0: use strict; rlm@0: use CGI::Ajax; rlm@0: use CGI; rlm@0: use DBI; rlm@0: rlm@0: my $q = new CGI; rlm@0: rlm@0: ### phone book database rlm@0: # CREATE TABLE `phonebook` ( rlm@0: # `login` varchar(10) NOT NULL, rlm@0: # `fullname` varchar(200) NOT NULL, rlm@0: # `areacode` int(10) unsigned NOT NULL default '123', rlm@0: # `phone` varchar(7) NOT NULL rlm@0: # ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Users and phone numbers'; rlm@0: # rlm@0: my $exported_fx = sub { rlm@0: my $searchterm = shift; rlm@0: my $sql = qq< select login from phonebook where login like ? or fullname like ? >; rlm@0: my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss'); rlm@0: my $sth = $dbh->prepare( $sql ); rlm@0: $sth->execute( $searchterm . '%', $searchterm . '%' ); rlm@0: rlm@0: # start off the div contents with select init rlm@0: my $html = qq!\n!; rlm@0: rlm@0: return($html); rlm@0: }; rlm@0: rlm@0: my $get_details = sub { rlm@0: my $login = shift; rlm@0: my $sql = qq< select * from phonebook where login = ? >; rlm@0: my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss'); rlm@0: my $sth = $dbh->prepare( $sql ); rlm@0: $sth->execute( $login ); rlm@0: rlm@0: my $html = ""; rlm@0: rlm@0: my $row = $sth->fetch(); rlm@0: if ( defined $row ) { rlm@0: $html .= "Login: " . $row->[0] . "
"; rlm@0: $html .= "Full Name: " . $row->[1] . "
"; rlm@0: $html .= "Area Code: " . $row->[2] . "
"; rlm@0: $html .= "Phone: " . $row->[3] . "
"; rlm@0: } else { rlm@0: $html .= "No Such User $login\n"; rlm@0: } rlm@0: return($html); rlm@0: }; rlm@0: rlm@0: rlm@0: my $Show_Form = sub { rlm@0: my $html = ""; rlm@0: $html .= < rlm@0: CGI::Ajax Example rlm@0: rlm@0: rlm@0: Who are you searching for?
rlm@0: Start typing and matches will display in the select box.
rlm@0: Selecting a match will give you details.  rlm@0:
rlm@0:
rlm@0:
rlm@0: rlm@0: EOT rlm@0: rlm@0: $html .= dump_table(); rlm@0: rlm@0: $html .= < rlm@0:
rlm@0:
rlm@0: rlm@0:
Show Source
rlm@0: rlm@0: rlm@0: rlm@0: EOT rlm@0: return $html; rlm@0: }; rlm@0: rlm@0: sub dump_table { rlm@0: my $sql = qq< select login from phonebook >; rlm@0: my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss'); rlm@0: my $sth = $dbh->prepare( $sql ); rlm@0: $sth->execute(); rlm@0: rlm@0: my $html = ""; rlm@0: rlm@0: while ( my $row = $sth->fetch() ) { rlm@0: $html .= ""; rlm@0: } rlm@0: rlm@0: $html .= "
Current Logins in DB
" . $row->[0] . "
"; rlm@0: return($html); rlm@0: } rlm@0: rlm@0: my $pjx = CGI::Ajax->new( rlm@0: search => $exported_fx, rlm@0: details => $get_details rlm@0: ); rlm@0: $pjx->JSDEBUG(1); rlm@0: $pjx->DEBUG(1); rlm@0: rlm@0: # not show the html, which will include the embedded javascript code rlm@0: # to handle the ajax interaction rlm@0: print $pjx->build_html($q,$Show_Form); # this outputs the html for the page