File: pjx_dynselect.pl

package info (click to toggle)
libcgi-ajax-perl 0.707-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 284 kB
  • sloc: perl: 1,669; makefile: 2
file content (130 lines) | stat: -rwxr-xr-x 3,325 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /usr/bin/perl -w

# dynamic select boxes, using a db

use strict;
use CGI::Ajax;
use CGI;
use DBI;

my $q = new CGI;

### phone book database
# CREATE TABLE `phonebook` (
#  `login` varchar(10) NOT NULL,
#  `fullname` varchar(200) NOT NULL,
#  `areacode` int(10) unsigned NOT NULL default '123',
#  `phone` varchar(7) NOT NULL
# ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Users and phone numbers';
#
my $exported_fx = sub {
	my $searchterm = shift;
	my $sql = qq< select login from phonebook where login like ? or fullname like ? >;
	my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss');	
	my $sth = $dbh->prepare( $sql );
	$sth->execute( $searchterm . '%', $searchterm . '%' );

	# start off the div contents with select init
	my $html = qq!<select name="users" id="users" style="width:440px;"
		onClick="details( ['users'],['ddiv'] ); return true;">\n!;


	my $firstrow = $sth->fetch();
	if ( defined $firstrow ) {
		$html .= qq!<option selected>! . $firstrow->[0] . qq!</option>\n!;
		
		# dot on each option from the db
		while ( my $row = $sth->fetch() ) {
			# $row->[0] will contain the login name
			$html .= qq!<option>! . $row->[0] . qq!</option>\n!;
		}

	}
	# close off the select and return
	$html .= qq!</select>\n!;

	return($html);
};

my $get_details = sub {
	my $login = shift;
	my $sql = qq< select * from phonebook where login = ? >;
	my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss');	
	my $sth = $dbh->prepare( $sql );
	$sth->execute( $login );

	my $html = "";

	my $row = $sth->fetch();
	if ( defined $row ) {
		$html .= "Login: " . $row->[0] . "<br>";
		$html .= "Full Name: " . $row->[1] . "<br>";
		$html .= "Area Code: " . $row->[2] . "<br>";
		$html .= "Phone: " . $row->[3] . "<br>";
	} else {
		$html .= "<b>No Such User $login</b>\n";
	}
	return($html);
};


my $Show_Form = sub {
  my $html = "";
  $html .= <<EOT;
<HTML>
<HEAD><title>CGI::Ajax Example</title>
</HEAD>
<BODY>
  Who are you searching for?<br>
	Start typing and matches will display in the select box.<br>
	Selecting a match will give you details.&nbsp;
	<br>
	<form>
  <input type="text" name="searchterm" id="searchterm" size="16"
	onkeyup="search( ['searchterm'], ['rdiv'] ); return true;"><br>

EOT

	$html .= dump_table();

	$html .= <<EOT;
	<div id="rdiv" style="border: 1px solid black; width: 440px;
		height: 80px; overflow: auto"></div>
	<br>
	<div id="ddiv" style="border: 1px solid black; width: 440px;
		height: 80px; overflow: auto"></div>

	<br><a href="pjx_dynselect.txt">Show Source</a><br>
	</form>
</BODY>
</HTML>
EOT
  return $html;
};

sub dump_table {
	my $sql = qq< select login from phonebook >;
	my $dbh = DBI->connect('dbi:mysql:test:localhost','guestuser','guestp4ss');	
	my $sth = $dbh->prepare( $sql );
	$sth->execute();

	my $html = "<table><tr><th>Current Logins in DB</th></tr>";

	while ( my $row = $sth->fetch() ) {
		$html .= "<tr><td>" . $row->[0] . "</td></tr>";
	}

	$html .= "</table>";
	return($html);
}

my $pjx = CGI::Ajax->new(
													search  => $exported_fx,
													details => $get_details
												);
$pjx->JSDEBUG(1);
$pjx->DEBUG(1);

# not show the html, which will include the embedded javascript code
# to handle the ajax interaction
print $pjx->build_html($q,$Show_Form); # this outputs the html for the page