Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

[Resolved]FormBuilder fails in "Multi screen mode"

by kazak (Beadle)
on Aug 23, 2012 at 09:41 UTC ( [id://989252]=perlquestion: print w/replies, xml ) Need Help??

kazak has asked for the wisdom of the Perl Monks concerning the following question:

Hi to all. I'm trying to build WebUI with help of CGI::FormBuilder, I managed to make it to process parameters for one "mode", but things turned to bad when I'm trying to add second more "mode", it continues to work with my first "mode", but dies even if I try to build a simple form with only necessary options. Here is some code and it works.
#!/usr/bin/perl -wT use strict; use warnings; use DBI; #use CGI; use CGI qw(:standard); use CGI::FormBuilder; use Data::Dumper; my (@keys,@values,@env_types); my ($keylist,$valuelist,$field,$env_choose); my $o_dbuser = "usr"; my $o_dbpwd = "pwd"; my ($query,$last,$servers_add); my $form = CGI::FormBuilder->new(keepextras => 1); my $mode = $form->cgi_param('mode'); #my $cgi = CGI->new(); #my $mode = $cgi->param('mode'); #my $mode = param('mode'); my $dbh = DBI->connect("DBI:mysql:database=overall;host=192.192.192.19 +2;port=3307", "$o_dbuser", "$o_dbpwd", {'RaiseError' => 1}); my $env_types = "SHOW TABLES FROM `overall` LIKE '%xen_env_%';"; my $sth = $dbh->prepare($env_types); $sth->execute() or die "could not execute", $sth->errstr; while (my $e = $sth->fetchrow_array) { push @env_types, $e; } $sth->finish(); if ($mode eq 'servers') { my $get_summary = "SELECT * FROM `Servers`;"; my $sth0 = $dbh->prepare($get_summary); $sth0->execute(); @keys = @{$sth0->{NAME_lc}} or die "Can't get Field names due +to:$!"; $sth0->finish(); $sth0 = $dbh->prepare($get_summary); $sth0->execute() or die "could not execute", $sth0->errstr; my $dbvalues = $sth0->fetchrow_hashref; $sth0->finish(); $last = $keys[$#keys]; foreach (@keys) { $keylist .= "$_,"; } $keylist =~ s/$last\,/$last/; $servers_add = CGI::FormBuilder->new( fields => [@keys], header => 1, method => 'POST', required => 'ALL', stylesheet => 'fb.css', keepextras => 1, ); $servers_add->field(name => 'env_type', options => [@env_ty +pes], type => 'select',); if ($servers_add->submitted) { $field = $servers_add->field; $last = $keys[$#keys]; for my $k (@keys) { $valuelist .= "'$field->{$k}',"; } $valuelist =~ s/\'\,$/\'/; my $sql = "INSERT INTO `Servers` ($keylist) VALUES($va +luelist);"; $sth0 = $dbh->prepare($sql); $sth0->execute() or die "Could not execute", $sth0->er +rstr; $sth0->finish(); print $servers_add->confirm; } else { print $servers_add->render; } }
So far so good, script dies in case his "mode" parameter is not defined or unknown, and it's correct. Script works as a charm if it used like: http://127.0.1.1/puppeteer/adder.cgi?mode=servers

But if I expanding this chunk of code, like this:

#!/usr/bin/perl -wT use strict; use warnings; use DBI; #use CGI; use CGI qw(:standard); use CGI::FormBuilder; use Data::Dumper; my (@keys,@values,@env_types); my ($keylist,$valuelist,$field,$env_choose); my $o_dbuser = "usr"; my $o_dbpwd = "pwd"; my ($query,$last,$servers_add); my $form = CGI::FormBuilder->new(keepextras => 1); my $mode = $form->cgi_param('mode'); #my $cgi = CGI->new(); #my $mode = $cgi->param('mode'); #my $mode = param('mode'); my $dbh = DBI->connect("DBI:mysql:database=overall;host=192.192.192.19 +2;port=3307", "$o_dbuser", "$o_dbpwd", {'RaiseError' => 1}); my $env_types = "SHOW TABLES FROM `overall` LIKE '%xen_env_%';"; my $sth = $dbh->prepare($env_types); $sth->execute() or die "could not execute", $sth->errstr; while (my $e = $sth->fetchrow_array) { push @env_types, $e; } $sth->finish(); if ($mode eq 'servers') { my $get_summary = "SELECT * FROM `Servers`;"; my $sth0 = $dbh->prepare($get_summary); $sth0->execute(); @keys = @{$sth0->{NAME_lc}} or die "Can't get Field names due +to:$!"; $sth0->finish(); $sth0 = $dbh->prepare($get_summary); $sth0->execute() or die "could not execute", $sth0->errstr; my $dbvalues = $sth0->fetchrow_hashref; $sth0->finish(); $last = $keys[$#keys]; foreach (@keys) { $keylist .= "$_,"; } $keylist =~ s/$last\,/$last/; $servers_add = CGI::FormBuilder->new( fields => [@keys], header => 1, method => 'POST', required => 'ALL', stylesheet => 'fb.css', keepextras => 1, ); $servers_add->field(name => 'env_type', options => [@env_ty +pes], type => 'select',); if ($servers_add->submitted) { $field = $servers_add->field; $last = $keys[$#keys]; for my $k (@keys) { $valuelist .= "'$field->{$k}',"; } $valuelist =~ s/\'\,$/\'/; my $sql = "INSERT INTO `Servers` ($keylist) VALUES($va +luelist);"; $sth0 = $dbh->prepare($sql); $sth0->execute() or die "Could not execute", $sth0->er +rstr; $sth0->finish(); print $servers_add->confirm; } else { print $servers_add->render; } }elsif ($mode eq 'env') { $env_choose = CGI::FormBuilder->new( fields => [qw(env_type env_state)], header => 1, method => 'post', keepextras => 1, ); if ($env_choose->submitted) { print $env_choose->confirm; } }
It just dies with error:

Thu Aug 23 12:29:04 2012] [error] [client 127.0.0.1] Premature end of script headers: adder.cgi

I just can't figure out why it happens and I would appreciate any help, thanks in advance.

Replies are listed 'Best First'.
Re: FormBuilder fails in "Multi screen mode"
by Anonymous Monk on Aug 23, 2012 at 09:57 UTC

    I just can't figure out why it happens and I would appreciate any help, thanks in advance.

    But you know FormBuilder is to blame?

    If you think FormBuilder is to blame, please write code that only uses FormBuilder, no DBI . You can use Data::Dump/Data::Dumper to get sample data.

Re: FormBuilder fails in "Multi screen mode"
by Anonymous Monk on Aug 23, 2012 at 10:00 UTC

    It just dies with error: Thu Aug 23 12:29:04 2012] [error] [client 127.0.0.1] Premature end of +script headers: adder.cgi

    Well, there should be something more in the log, either following or preceeding that -- what does the program output when you run it outside of browser ,from bash or other shell?

    See CGI Help Guide and Troubleshooting Perl CGI scripts

      Well it's the reason why I'm here - nothing more in an error.log and nothing is preceding this error. As you see I've added "use DataDumper" into my code in order to see what may be wrong with variables, in particular with "$mode" but for me, it seems correct: "mode=servers" or "mode=env" but please correct me if I wrong.

        As you see I've added "use DataDumper" into my code in order to see what may be wrong with variables, in particular with "$mode" but for me, it seems correct: "mode=servers" or "mode=env" but please correct me if I wrong.

        Sorry, I can't. I don't have a mysql database .... the idea is for you to use Dumper to generate sample data, so you can run the program without a database , I mean I can run the program without a database

Re: FormBuilder fails in "Multi screen mode"
by Anonymous Monk on Aug 23, 2012 at 12:27 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found