 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

September 13th, 2010, 08:38 AM
|
Registered User
|
|
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Create a Combobox in html
Actually I have a CGI form which consists of
textfields and I need a combobox in which I can
enter my own data dynamically. May be it seems very
silly question but I am new to cgi-perl as well as
HTML so no idea what to do. Here is my form:
#!C:\perl\bin\perl.exe
use CGI;
use CGI qw/:standard/;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $q = new CGI;
use DBI;
use CGI qw(:all);
use strict;
use warnings;
print "Content-Type: text/html\n\n";
print $q->header ( );
if ( $q->param("submit") )
{
process_form ( );
}
else
{
display_form ( );
}
sub process_form
{
if ( validate_form ( ) )
{
display_form ( );
}
}
sub validate_form
{
my $User_Name = $q->param("User_Name");
my $User_Password= $q->param("User_Password");
my $User_Permission = $q->param("User_Permission");
my $User_Department= join(", ",$q->param
("User_Department"));
my $error_message = "";
$error_message .= "Please enter your name<br/>" if
( !$User_Name );
$error_message .= "Please enter your Password<br/>"
if( ! $User_Password );
$error_message .= "Please Select a permission<br/>"
if( !$User_Permission );
$error_message .= "Please select atleast 1
department<br/>" if(!$User_Department);
if ( $error_message )
{
display_form (
$error_message,$User_Name,$User_Password,$User_Per mission
,$User_Department);
return 0;
}
else
{
my $dbh = DBI->connect
("dbi:SQLite:DEVICE.db","", "",{RaiseError => 1,
AutoCommit =>
1 } );
my $sql = "SELECT COUNT(UserName) FROM UsersList
WHERE UserName='$User_Name'";
my $sth = $dbh->prepare($sql) or die("\n\nPREPARE
ERROR:\n\n$DBI::errstr");
$sth->execute or die("\n\nQUERY
ERROR:\n\n$DBI::errstr");
my ($n) = $dbh->selectrow_array($sth);
$sth->finish();
if ($n > 0) {
print "Record Already Exists";
}
else {
my $sql = "INSERT INTO UsersList
(UserName,Password,Permission,Department) VALUES
('$User_Name ','
$User_Password','$User_Permission','$User_Departme nt')";
my $sth = $dbh->prepare($sql);
$sth->execute;
print "Record Added Successfully";
$sth->finish();
$dbh->commit or die $dbh->errstr;
}
$dbh->disconnect;
}
}
sub display_form
{
my $error_message = shift;
my $User_Name = shift;
my $User_Password = shift;
my $User_Permission= shift;
my $User_Department= shift;
my $User_Permission_Add_sel = $User_Permission
eq "Add" ? " checked" : "";
my $User_Permission_Edit_sel =$User_Permission
eq "Edit" ? " checked" : "";
my $User_Permission_Delete_sel =$User_Permission
eq "Delete" ? " checked" : "";
my $User_Permission_View_sel =$User_Permission
eq "View" ? " checked" : "";
my $User_Department_html = "";
my $dbh = DBI->connect
("dbi:SQLite:DEVICE.db","", "",{RaiseError => 1,
AutoCommit =>
1 } );
my $sql = "select DepartmentName from Departments
order by DepartmentName";
my $sth = $dbh->prepare($sql);
$sth->execute() ;
while (my $User_Department_option= $sth-
>fetchrow_array)
{
$User_Department_html.= "<option
value=\"$User_Department_option\"";
$User_Department_html.= " selected" if (
$User_Department_option eq
$User_Department );
$User_Department_html.= ">$User_Department_option</option
>";
}
$sth->finish();
$dbh->commit or die $dbh->errstr;
print <<END_HTML;
<html>
<head><title>Form Validation</title></head>
<body>
<form action="AddUser.cgi" method="post">
<input type="hidden" name="submit" value="Submit">
<p>$error_message</p>
<TABLE BORDER="1" align="center">
<TR>
<TD>Name</TD>
<TD> <input type="text" name="User_Name"
value="$User_Name"></TD>
</TR>
<TR>
<TD>Password</TD>
<TD colspan="2"><input type="password"
name="User_Password" value="$User_Password"
size="20" maxlength="15" /></TD>
</TR>
<TR>
<TD>Role</TD>
<TD>"HERE I NEED A COMBOBOX"</TD>
</TR>
<TR>
<TD>Permission</TD>
<TD><input type="radio" name="User_Permission"
value="Add"$User_Permission_Add_sel>Add<input
type="radio" name="User_Permission"
value="Edit"$User_Permission_Edit_sel>Edit<input
type="radio"
name="User_Permission"
value="Delete"$User_Permission_Delete_sel>Delete<i nput
type="radio" name="User_Permission"
value="View"$User_Permission_View_sel>View</TD>
</TR>
<TR>
<TD>Department</TD>
<TD colspan="2"> <select name="User_Department"
MULTIPLE
SIZE=4>$User_Department_html</select></TD>
</TR>
</TR>
<TR>
<TD align="center" colspan="2">
<input type="submit" name="submit" value="ADD">
</TD>
</TR>
</TABLE
</form>
</body></html>
END_HTML
}
|

September 14th, 2010, 07:48 AM
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 23
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
1. Use placeholders
2. Read "Learning Perl" or "Beginning Perl"
3. To create multiple select you can use $q->scrolling_list
4. There is no combo box, but you can use text field and scrolling list.
I formatted your code with perltidy and done some changes in the code. Next time please use [ CODE ] tags in this forum.
Code:
#!C:\perl\bin\perl.exe
use strict;
use warnings;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $q = CGI->new;
use DBI;
print $q->header();
if ( $q->param("submit") ) {
process_form();
}
else {
display_form();
}
sub process_form {
if ( validate_form() ) {
display_form();
}
}
sub validate_form {
my $User_Name = $q->param("User_Name");
my $User_Password = $q->param("User_Password");
my $User_Permission = $q->param("User_Permission");
my $User_Department = join( ", ", $q->param("User_Department") );
my $error_message = "";
$error_message .= "Please enter your name<br/>"
if ( !$User_Name );
$error_message .= "Please enter your Password<br/>"
if ( !$User_Password );
$error_message .= "Please Select a permission<br/>"
if ( !$User_Permission );
$error_message .= "Please select at least 1 department<br/>" if ( !$User_Department );
if ($error_message) {
display_form(
$error_message, $User_Name, $User_Password,
$User_Permission, $User_Department
);
return 0;
}
else {
my $dbh = DBI->connect(
"dbi:SQLite:DEVICE.db",
"", "",
{
RaiseError => 1,
AutoCommit => 1
}
);
my $sql = "SELECT COUNT(UserName) FROM UsersList
WHERE UserName=?";
my $sth = $dbh->prepare($sql) or die(
"\n\nPREPARE
ERROR:\n\n$DBI::errstr"
);
$sth->execute($User_Name) or die(
"\n\nQUERY
ERROR:\n\n$DBI::errstr"
);
my ($n) = $dbh->selectrow_array($sth);
$sth->finish();
if ( $n > 0 ) {
print "Record Already Exists";
}
else {
my $sql = "INSERT INTO UsersList
(UserName,Password,Permission,Department) VALUES (?,?,?,?)";
my $sth = $dbh->prepare($sql);
$sth->execute($User_Name,$User_Password,$User_Permission,$User_Department);
print "Record Added Successfully";
$sth->finish();
$dbh->commit or die $dbh->errstr;
}
$dbh->disconnect;
}
}
sub display_form {
my $error_message = shift;
my $User_Name = shift;
my $User_Password = shift;
my $User_Permission = shift;
my $User_Department = shift;
my $User_Permission_Add_sel = $User_Permission eq "Add" ? " checked" : "";
my $User_Permission_Edit_sel = $User_Permission eq "Edit" ? " checked" : "";
my $User_Permission_Delete_sel =
$User_Permission eq "Delete" ? " checked" : "";
my $User_Permission_View_sel = $User_Permission eq "View" ? " checked" : "";
my $User_Department_html = "";
my $dbh = DBI->connect(
"dbi:SQLite:DEVICE.db",
"", "",
{
RaiseError => 1,
AutoCommit => 1
}
);
my $sql = "select DepartmentName from Departments
order by DepartmentName";
my $sth = $dbh->prepare($sql);
$sth->execute();
while ( my $User_Department_option = $sth->fetchrow_array ) {
$User_Department_html .= "<option
value=\"$User_Department_option\"";
$User_Department_html .= " selected"
if ( $User_Department_option eq $User_Department );
$User_Department_html .= ">$User_Department_option</option
>";
}
$sth->finish();
$dbh->commit or die $dbh->errstr;
print <<END_HTML;
<html>
<head><title>Form Validation</title></head>
<body>
<form action="AddUser.cgi" method="post">
<input type="hidden" name="submit" value="Submit">
<p>$error_message</p>
<TABLE BORDER="1" align="center">
<TR>
<TD>Name</TD>
<TD> <input type="text" name="User_Name"
value="$User_Name"></TD>
</TR>
<TR>
<TD>Password</TD>
<TD colspan="2"><input type="password"
name="User_Password" value="$User_Password"
size="20" maxlength="15" /></TD>
</TR>
<TR>
<TD>Role</TD>
<TD>"HERE I NEED A COMBOBOX"</TD>
</TR>
<TR>
<TD>Permission</TD>
<TD><input type="radio" name="User_Permission"
value="Add"$User_Permission_Add_sel>Add<input
type="radio" name="User_Permission"
value="Edit"$User_Permission_Edit_sel>Edit<input type="radio"
name="User_Permission" value="Delete"$User_Permission_Delete_sel>Delete<input
type="radio" name="User_Permission"
value="View"$User_Permission_View_sel>View</TD>
</TR>
<TR>
<TD>Department</TD>
<TD colspan="2"> <select name="User_Department"
MULTIPLE
SIZE=4>$User_Department_html</select></TD>
</TR>
</TR>
<TR>
<TD align="center" colspan="2">
<input type="submit" name="submit" value="ADD">
</TD>
</TR>
</TABLE
</form>
</body></html>
END_HTML
}
|
|
 |