Hi all,
I'have almost no experience in programming or writing CGI Scripts so
perhaps my question isn't very clever.
My problem concern the evaluation of From Input. What's the best way to
check wheather a field contains input or not. What I want to do, is to set
up a form that has to be filled out and its content is then sent to a database.
The connection to the database works fine, in fact to good. Everytime the
script is called a new empty set of data is inserted into the database. Not
exactly what I want. I have tried to use a foreach loop and a if/else loop
in the subroutine, but none of it worked.
It either always (e.g when I call it for the first time) tells me that I
haven't filled out the form correctly or it never does. Never the less it
alway inserts something into the database.
Probably I got it all wrong but can someone give me some general advise how
to handle such problems? Use a JavaScript to check the input?
Pretty confused
Marcus
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp('fatalsToBrowser');
my ($vname, $nname);
my $query = new CGI;
print $query->header;
print $query->start_html("TEST SUB");
&print_form($query);
&check_fields($query);
print $query->end_html;
sub print_form{
print "<B>Test Sub</b>";
print $query->startform;
print "Ihr Vorname?";
print $query->textfield('vname');
print "Ihr Nachname?";
print $query->textfield('nname');
print "<P>";
print $query->submit('action','Los');
print $query->reset;
print $query->endform;
}
sub check_fields{
my @values = $query->param;
if($value(@values) == 0) {
print "Some Text";
}else{
insert_db($query)
}
}
sub insert_db{
my $vname = $query->param('vname');
my $nname = $query->param('nname');
use DBI;
my $dbh = DBI->connect("DBI:mysql:Abonnenten","root","")
or die "Error: $DBI::errstr\n";
my $sql = "INSERT INTO namen (nname, vname) VALUES ('$nname','$vname')";
my $sth = $dbh->prepare($sql);
$sth->execute;
$dbh->disconnect;
}