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