Click to See Complete Forum and Search --> : Redirect


Gunner
08-06-2003, 05:25 AM
I found a script that sends info from a form to a flat file then sends the user to a success or failure page generated by the script and it has worked great for a couple of years. I am trying to mod it to redirect the user to pages I have written.
I have done some research and found several ways of doing it, My problem is I cant get any to work. Here is what I have so far:

# this is where the info will be written to - you need to specify a real directory
$file ="/path/to/results.txt"; #must be read/writable

#
##################################################################

if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
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;
$contents{$name} = $value;

}
}

chop($date = `date`);

# Now with the program
###########################################################
# Has to output a Content-type
print "Content-type: text/html\n\n ";

# Check to see if all required information was entered
# If you want a field to be required, add it here.
&no_cigar unless $contents{'comments'};


sub no_cigar
{
print $query->redirect(-uri=>'http://www.yourdomain.com/failure.html', -nph=>1);
exit;
}



# They go here if the form was submitted
# successfully.

print $query->redirect(-uri=>'http://www.yourdomain.com/success.html', -nph=>1);

#Prints to the file

#print "Content-type: text/plain\n\n ";
open(OUTPUT, ">>$file");
print OUTPUT "_______________________________\n";
print OUTPUT "Date: $date\n";
print OUTPUT "FORM NAME: $contents{'formname'}\n";
print OUTPUT "COMMENT: $contents{'comments'}\n";




close (OUTPUT);
exit;

All I get is a blank page and the address bar still points to the script!

Any help would be great!

Jeff Mott
08-06-2003, 10:05 PM
print $query->redirect(-uri=>'http://www.yourdomain.com/failure.html', -nph=>1);Try changing this to...print $query->redirect('http://www.yourdomain.com/failure.html');...in both cases.

Gunner
08-06-2003, 10:40 PM
Thanks for the help, but that didnt work either, so I tried this:

sub no_cigar
{
print <<"HTML";
<html><head><title>Form Incomplete</title>
<meta HTTP-EQUIV="REFRESH" CONTENT="1; URL=http://www.thegunnersgallery.com/oops.html">
</head>
<body>
</body></html>
HTML
exit;
}



# They go here if the form was submitted
# successfully.
print <<"HTML";
<html><head><title>Success</title>
<meta HTTP-EQUIV="REFRESH" CONTENT="1; URL=http://www.thegunnersgallery.com/success.html">
</head>
<body>
</body></html>
HTML

and that seems to work.

Thanks again!

If you see anything wrong with it let me know.

Jeff Mott
08-06-2003, 10:48 PM
What error were you getting when it wasn't working?

Gunner
08-06-2003, 11:09 PM
I wasnt getting an error just a blank page with the url still pointing to the script. When I viewed the source it was blank too.

goofball
08-18-2003, 04:19 PM
I know I can't see the top part of your script, but are you sure you built the query object right?

what I do is just import part of CGI like:
use CGI ':standard';
and then you can just use:
print redirect("http://www.domain.com"); without having to worry about going through a query object.

You could also skip that whole block at the top-
about reading the CONTENT_LENGTH through STDIN and splitting it into name/value pairs.
When you use CGI ':standard' you already have those variables in the param() array, so you could do:
foreach(param()) {
$contents{$_} = param($_);
}