Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

.cgi interaction problems

by kazak (Beadle)
on Oct 01, 2012 at 12:33 UTC ( #996641=perlquestion: print w/ replies, xml ) Need Help??
kazak has asked for the wisdom of the Perl Monks concerning the following question:

Hi to all community, I'm stuck and now I have feeling that my script contains two types of errors, for some of them I have enough of experience to see and fix, and some of them I'm even unable to see, so I want to ask for help. I want to build a cgi that checks out a DB builds a table and provides buttons\checkboxes\etc for different modifications. I have a cgi for editing of this table and it works if I'm using it manually like: http://127.0.1.1/puppeteer/edit.cgi?mode=stats&field=site&value=test1.test.com At this time I'm trying to make a call an "edit.cgi" by clicking "Edit" button, and it's not working.

main.cgi

#!/usr/bin/perl -wT use strict; use warnings; use DBI; use CGI qw(:standard); use Date::Format; use HTML::Template; my (@keys,@values,@env_types,@servers,@loopdata); my %rowdata; my ($env_types); my $o_dbuser = "test"; my $o_dbpwd = "test"; my $cgi = CGI->new(); my $mode = $cgi->param('mode') || 'show'; my $dbh = DBI->connect("DBI:mysql:database=overall;host=10.10.10.10;po +rt=3306", "$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(); my $get_servers = "SELECT `serverid` FROM `Servers`;"; my $sth0 = $dbh->prepare($get_servers); $sth0->execute(); while (my $s = $sth0->fetchrow_array()) { push @servers, $s; } $sth0->finish(); if ($mode eq 'servers') { my (@fields); 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(); push @fields, "<th BGCOLOR=\"\#FFFFFF\">flag</th>\n"; push @fields, "<th BGCOLOR=\"\#C8C8E8\"> </th>\n"; for (@keys) { push @fields, "<th BGCOLOR=\"\#C8C8E8\"><TMPL_VAR NAME +=$_></th>\n"; } for (@keys) { if ($_ eq "serverid") { push @values,"<tr><td align=\"center\"><input +type=\"checkbox\" name=\"choice\" value=\"<TMPL_VAR NAME=serverid>\"> + </td>\n"; push @values,"<form action=\"edit.cgi\" method +=\"get\"><td align=\"center\" BGCOLOR=\"\#BFE4FD\" ><input type=\"sub +mit\" name=\"mode\" value=\"edit\"></td></form>\n"; } push @values, "<td width=15% align=\"center\" BGCOLOR= +\"\#BFE4FD\" border-style=solid white><TMPL_VAR NAME=$_></td>\n"; } open (TMPL,">", "/tmp/servers.tmpl"); print TMPL <<SERVERS; <html> <head> <style type="text/css"> p {font-size: 20pt; font-family: "times new roman"; bo +rder-style:solid ; border-width:1px;} p.one {font-size: 8pt; font-family: "times new roman"; + border-style:solid black;} body { font-family: verdana,arial,sans-serif; font-size: 5pt; } </style> </head> <body> <form> <p class="one"> <table border=0 > <TMPL_LOOP NAME=KEYS> @fields </TMPL_LOOP> <TMPL_LOOP NAME=VALUES> @values </TMPL_LOOP> <table></table> </p> </form> </body> </html> SERVERS close(TMPL); foreach my $key (@keys) { $rowdata{$key} = $key; } push(@loopdata, \%rowdata); my $values; $get_summary = "SELECT * FROM `Servers`;"; $sth0 = $dbh->prepare($get_summary); $sth0->execute(); push @{$values}, $_ while $_ = $sth0->fetchrow_hashref(); $sth0->finish(); my $template = HTML::Template->new(filename => '/tmp/servers.t +mpl', associate => $cgi,); $template->param( KEYS => \@loopdata); $template->param( VALUES => $values); print "Content-Type: text/html\n\n", $template->output; }

HTML generated by my script:

<html> <head> <style type="text/css"> p {font-size: 20pt; font-family: "times new roman"; border-sty +le:solid ; border-width:1px;} p.one {font-size: 8pt; font-family: "times new roman"; border- +style:solid black;} body { font-family: verdana,arial,sans-serif; font-size: 5pt; } </style> </head> <body> <form> <p class="one"> <table border=0 > <th BGCOLOR="#FFFFFF">flag</th> <th BGCOLOR="#C8C8E8"> </th> <th BGCOLOR="#C8C8E8">serverid</th> <th BGCOLOR="#C8C8E8">env_type</th> <th BGCOLOR="#C8C8E8">predicted_capacity</th> <th BGCOLOR="#C8C8E8">used_capacity</th> <th BGCOLOR="#C8C8E8">available_space</th> <th BGCOLOR="#C8C8E8">free_space</th> <th BGCOLOR="#C8C8E8">percents_free</th> <th BGCOLOR="#C8C8E8">daily_load</th> <th BGCOLOR="#C8C8E8">applied_routine</th> <th BGCOLOR="#C8C8E8">comments</th> <th BGCOLOR="#C8C8E8">check_date</th> <tr><td align="center"><input type="checkbox" name="choice" va +lue="xen1-test1"> </td> <form action="edit.cgi" method="get"><td align="center" BGCOLOR="#BFE +4FD" ><input type="submit" name="mode" value="edit"></td></form> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>xen1-test1</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>xen_env_app</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>500</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>None</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>2012-08-22</td> <tr><td align="center"><input type="checkbox" name="choice" va +lue="xen1-test2"> </td> <form action="edit.cgi" method="get"><td align="center" BGCOLOR="#BFE +4FD" ><input type="submit" name="mode" value="edit"></td></form> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>xen1-test2</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>xen_env_app</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>500</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>486</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>492</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>211</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>43</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>904</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>0</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>None</td> <td width=15% align="center" BGCOLOR="#BFE4FD" border-style=solid whi +te>2012-08-21</td> <table></table> </p> </form> </body> </html>

So from my point of view I see following problems:

1. What type of requests should I use POST\GET and how I should change my scripts in order to make them work in a same way? (I'm using CGI)

2. Clicking on "Edit" button I'm getting: http://127.0.1.1/puppeteer/main.cgi?... instead of http://127.0.1.1/puppeteer/edit.cgi?... defined on action="edit.cgi" of a form tag. Can anyone tell why this is happening?

<form action="edit.cgi" method="get"><td align="center" BGCOLOR="#BFE4FD" ><input type="submit" name="mode" value="edit"></td></form>

Comment on .cgi interaction problems
Select or Download Code
Re: .cgi interaction problems
by Anonymous Monk on Oct 01, 2012 at 12:53 UTC

    some happy thoughts, laugh :)

    Well, we only have your word that it isn't happening, it looks like it should happen ...

    but you never check to see if you can write to template file ...

    writing to tempate file in temp directory is weird and unneeded, perldoc HTML::Template | grep new

    new_scalar_ref or scalarref => \$template

    \"\"\"\" is PAINFUL, use qq{""""}

    fetchrow_hashref docs warn you about storing the reference but you store it anyway -- your data is all duplicates -- might as well fetchall instead Tutorials->Database Programming->DBI recipes

    I've been up all night :D

      I'm glad that my "code" made you laugh. To post a code written by a newbie and expect that none will laugh is too naive even for a child, so it's ok, anyway thanks for advices.

      About: writing to /tmp...

      I made it for debugging purposes. Sure it may be unneeded but at this point I'm using things I'm familiar with.

      About:\"\"\"\" is PAINFUL, use qq{""""}

      Thx, I'll use it.

      About: fetchrow_hashref docs warn you about storing the reference but you store it anyway -- your data is all duplicates -- might as well fetchall instead Tutorials->Database Programming->DBI recipes

      Thanks for a clue, I will read about it

        I'm glad that my "code" made you laugh. To post a code written by a newbie and expect that none will laugh is too naive even for a child, so it's ok, anyway thanks for advices.

        Let me rephrase, "here are some happy thoughs for you kazak, you should laugh :)"

        The universe is full of vibes, catch a good one :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-04-20 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls