Gossamer Forum
Home : General : Perl Programming :

Why won't this work?

Quote Reply
Why won't this work?
Hi. I am trying to write an installation script for one of my scripts, but i am having trouble with it :) I have managed to write the script correctly except for showing the correct sub routine for the basic page, and then if they have submitted the form. As you can see i have had a go myself, but it didn't work very well! I just kept on getting the main sub-routine (sub start_page). Any ideas on how i can stop this happening would be much appreciated! To see what i mean please look at http://www.ace-installer.com/cgi-bin/install.cgi

Andy Cool

The script is below;

##############################################
# CODE
##############################################

#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Break em up into a format the script can read

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

$action = $FORM{action};

if ($action ne "setup") { &start_page } else { &setup_script }

#########################################################
# FIRST SUB FOR START PAGE #
#########################################################

sub start_page
{
print "Content-type: text/html \n\n";
print qq|

<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Install Script for Ace Submit</title>
</head>

<body>

<form method="GET" action="install.cgi?action=setup">
<p>URL Site : <input type="text" name="urlsite" size="20"></p>
<p>Title :         <input type="text" name="title" size="20"></p>
<p>Port No:    <input type="text" name="portno" size="3" value="80">
<p>Date Location:    <input type="text" name="date" size="20" value="/bin/date">
<p>Database Location:    <input type="text" name="database" size="20" value="database.txt">
</p>
<p>Save E-Mails?  <select size="1" name="saveemailvalue">
<option value="1">Yes</option>
<option value="0">No</option>
</select></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>

|;

}



###################################################
# SECOND SUB #
###################################################

sub setup_script
{

$home1 = "$FORM{urlsite}";
$org1 = "$FORM{title}";
$portno1 = "$FORM{portno}";
$saveemail = "$FORM{saveemailvalue}";
$date1 = "$FORM{date}";
$database1 = "$FORM{database}";

open(TOFILE,">settings.cfg");
flock(TOFILE,2);

print TOFILE "\#URL Path to your site \n";
print TOFILE "\$home = \"$home1\";\n";
print TOFILE "\n";
print TOFILE "\# Change to your Site Name\n";
print TOFILE "\$org = \"$org1\"\;\n";
print TOFILE "\n";
print TOFILE "\n";
print TOFILE "\$port = \"$portno1\"; \n";
print TOFILE "\n";
print TOFILE "\$saveemails = \"$saveemail\"\; \# 1 is yes, 0 is no \n";
print TOFILE "\n";
print TOFILE "\$date = \`$date1\`\; chop (\$date)\;\n";
print TOFILE "\n";
print TOFILE "\$database = \"$database1\"\;\n";


close(TOFILE);

print "Content-type: text/html \n\n";

print qq|

<html>

<head>
<title>Setup Done</title>
</head>

<body>

<p align="center">settings.cfg File has been generated. Now use the demo code made available in the zip file and change the location of the script in the HTML coding (http://www.ace-installer.com/cgi-bin/acesubmit.html). Don't change the values of the form boxes otherwise it won't work!"</a></p>

</body>

</html>

|;


}


##################################################
# END OF CODE
##################################################

Quote Reply
Re: Why won't this work? In reply to
I hope you dont mind but I took your code and modified it to use it as a feedback form.

I am using the following code:

#!/usr/bin/perl


if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

$action = $FORM{action};

if ($action ne "email") {
&start_page
} else {
&email_it
}


sub start_page
{
print "Content-type: text/html \n\n";
print qq|

<FORM ACTION="form.cgi?action=email" METHOD="GET">

<HTML FORM GOES HERE>

</form>

|;

}

sub email_it {

$name = "$FORM{requiredName}";
$age = "$FORM{requiredAge}";
$email = "$FORM{requiredEmail}";
$country = "$FORM{requiredCountry}";
$comments = "$FORM{requiredComments}";


open(MAIL,"|/usr/sbin/sendmail -t"); {
print MAIL "To: paul\@audio-grabber.com\n";
print MAIL "From: $email\n";
print MAIL "Subject: Audio-Grabber Feedback\n\n" }
print MAIL "This feedback for was submitted by $name\n";
print MAIL "$name\n";
print MAIL "$age\n";
print MAIL "$email\n";
print MAIL "$country\n";
print MAIL "$comments\n";
print MAIL "-" x 75 . "\n\n";


print "Content-type: text/html \n\n";

print qq|

<html>

<head>
<title>Thanks for your feedback!</title>
</head>

<body>

<p align="center">Sent!</a></p>
<p>$name<P>$age<p>$email<p>$country<p>$comments
</body>
</html>
|;
}


I seem to be experiencing the same problem as you. Even though you post the script to form.cgi?action=email I still end up on the start page when I press submit.

However if I go to /cgi-bin/form.cgi?action=email in my browser, the email gets sent to me.

Any ideas?
Should it be METHOD=POST ?

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Why won't this work? In reply to
I don't know, but that is the same problem as i was having. Comeon, someone must know what we are doing wrong Smile

Andy

Quote Reply
Re: Why won't this work? In reply to
umm.. it looks like it is working.. but i don't understand why you'd do :

if ($action ne "setup") { &start_page } else { &setup_page }

it would make more sense as..

if ($action eq "setup") { &setup_page (); } else { &start_page (); }

also.. that brings up why it would reload over and over again..

you didn't have a form input "action"... in the start page form.. you'd need..

<input type="hidden" name="action" value="setup">

Jerry Su
widgetz sucks
Quote Reply
Re: Why won't this work? In reply to
It works now, all you need to do is add a hidden value for 'action' in the HTML code. For some reason it doesn't like it if you put it directly into the action query string Smile

Andy

Quote Reply
Re: Why won't this work? In reply to
because if you put it in the query string it gets overridden as a POST not a GET.. and the $FORM{action} because you have code that checks if its a GET or POST.. if its a GET it gets all the ones in the query_string.. but action isn't in there.. because its part of the URL now that you put it in the METHOD of the form

Jerry Su
widgetz sucks