Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How do I use a database and handle checkboxes?

by Ovid (Cardinal)
on Aug 22, 2000 at 06:51 UTC ( [id://28960]=note: print w/replies, xml ) Need Help??


in reply to How do I use a database and handle checkboxes?

There are many ways of addressing this issue. The following is not the best, but it works. I have a form where teachers can enter lesson plans and a series of check boxes represent the appropriate grade levels, K through 12. The checkboxes are named GradeK, Grade1, ... Grade12.

When this data is sent via CGI, the parameter is sent to the script as GradeK=on (or whatever value you set the checkbox at). No checkbox parameter is sent if it's not checked. Therefore, I use the following sub to pull the data:

sub getGrades { my $grades; my @keys = $template->param; # Look for the grade parameters and add each one to $grades foreach (@keys) { $grades .= $1 . ',' if /^Grade([1-9]|1[0-2]|K)$/; } $grades =~ s/,$//; # remove trailing comma return $grades; }
This sub is called from the following sub which uses DBI to write the data to a MySQL database:
sub storeData { my $table = shift; my $grades = getGrades(); my $standards = getStandards(); use DBI; my $dbh = DBI->connect("DBI:mysql:$database",$user,$password); $dbh->{RaiseError} = 1; # save template information to database my $sql = 'INSERT INTO ' . $table . '(title, lesson_summary, grades, standards) VALUES (?, ?, ?, ?)'; my $sth = $dbh->prepare($sql); $sth->execute( $template->param('Title'), $template->param('Summary'), $grades, $standards ); $template->end_html; $sth->finish; $dbh->disconnect; print $template->redirect('http://www.someserver.com/some/path/tha +nks.html'); }
This should give you a good start on how to approach this (Grades is defined as varchar(28), by the way).

Cheers,
Ovid

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28960]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-03-28 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found