View Full Version : if's problem
GregT
December 18th, 2002, 01:24
hi, im writing this script for a hosting company, but i can't figure this part out. how can i get it so that it will redirect based on whats in $input{pkg} ? here's my code atm.
# Now to redirect user to correct order page.
if ($input{pkg} == "Bronze") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=bronze.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "Silver") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=silver.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "Gold") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=gold.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "cBronze") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=cbronze.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "cSilver") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=csilver.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "cGold") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=cgold.html">
</head>
<body></body>
</html>
ENDOFTEXT
}else{ print "----, something went wrong !";}
JdS
December 18th, 2002, 04:10
use :
if{}
elseif{}
elseif{}
elseif{}
else{}
// or
switch()
{}
btw... check your 'here doc syntax'
GregT
December 18th, 2002, 14:30
there is no elseif in perl correct ? should be elsif :confused:
JdS
December 18th, 2002, 14:46
sorry buddy, I thought I was posting on phpbuilder or something :)
gave up on perl a LONG time ago...
my apologies
YUPAPA
December 18th, 2002, 16:10
There are if, elsif, and else in Perl
Your problem should be the 'if' statement
if ($input{pkg} == "Bronze") {
print <<'ENDOFTEXT';
<html>
<head>
change if($input{pkg} == "Bronze") to if($input{pkg} eq 'Bronze')
same with the others :)
YUPAPA
December 18th, 2002, 16:42
Here is a shorter one with the same header and footer... easier to customized
you can access to the script like this
http://www.yourdomain.com/pkg.pl?pkg=Bronze
Also, edit %pkgs...
if pkg is 'Bronze', then it will go to the 'bronze.html'
same for 'Ababa'. If pkg is 'Ababa', it will go to 'ababa.html'
#!/usr/bin/perl
use strict;
use CGI qw(:standard :form);
my $query = new CGI();
my $pkg = $query->param('pkg');
my %pkgs = ('Bronze' => 'bronze.html', 'Ababa' => 'ababa.html');
sub header {
my $header = q(
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=<REPLACEMENT>">
</head>
<body>
);
return $header;
}
sub footer {
my $footer = q(
</body>
</html>
);
return $footer;
}
sub print_page {
my $header = header();
print "Content-Type: text/html\n\n";
if($pkgs{$_[0]}) {
$header =~ s/\<REPLACEMENT\>/$pkgs{$_[0]}/g;
print $header;
print "Redirecting\n";
} else {
$header =~ s/\<meta http-equiv=\"refresh\"(.*)//g;
print $header;
print "No Such Package\n";
}
print footer();
}
print_page($pkg);
__END__
GregT
December 18th, 2002, 18:23
problem is that there is much more code than what i posted, so that wouldn't work. i'll post the code later when i go back to my desktop.
GregT
December 18th, 2002, 18:49
whole code:
#!/usr/bin/perl
require "cgi.pl";
MAIN:
{
# Read in all the variables set by the form
if (&ReadParse(*input)) {
&ProcessForm;
} else {
&PrintForm;
}
}
sub ProcessForm {
# Do some processing, and print some output
($text = $input{'text'}) =~ s/\n/\n<BR>/g;
# ($text = $input{'text}) =~ s/@/\@/g;
# add <BR>'s after carriage returns
# to multline input, since HTML does not
# preserve line breaks
# then search for '@' signs and replace with \@ so sendmail doesnt die.
$from = "$input{email}";
$to = "order\@total-host.net";
$subject = "Order for $input{pkg} by $input{firstname} $input{surname}";
$message = "New order for $pkg package.\nFirst name: $input{firstname}\nSurname: $input{surname}\nAddress: $input{addy}\nZip\/Post Code: $input{zpc}\nPhone Number: $input{phone}\nEmail: $input{email}\nDesired Username: $input{username}\nDesired Password: $input{password}\nMain Domain: $input{domain}\nPackage: $input{pkg}";
unless(open (MAIL, "|/usr/sbin/sendmail -t")) {
print "error.\n";
warn "Error starting sendmail: $!";
}
else{
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: test $subject\n\n";
print MAIL "$message";
close(MAIL) || warn "Error closing mail: $!";
}
# Now to redirect user to correct order page.
if ($input{pkg} == "Bronze") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=bronze.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "Silver") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=silver.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "Gold") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=gold.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "cBronze") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=cbronze.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "cSilver") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=csilver.html">
</head>
<body></body>
</html>
ENDOFTEXT
}
if ($input{pkg} == "cGold") {
print <<'ENDOFTEXT';
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url=cgold.html">
</head>
<body></body>
</html>
ENDOFTEXT
}else{ print "----, something went wrong !";}
# Close the document cleanly.
print &HtmlBot;
}
sub PrintForm {
print &PrintHeader;
# Print out the body of the form from a single quoted here-document
# Note that if ENDOFTEXT weren't surrounded by single quotes,
# it would be necessary to be more careful with the other text
# For example, the @ sign (in the address) would need to be escaped as \@
print <<'ENDOFTEXT';
<form method="post" action="order.cgi">
<H3> Total-Host Order form: </H3>
First name: <input name="firstname"><P>
Surname: <input name="surname"><P>
Address: <input name="addy"><P>
Zip/Postal Code: <input name="zpc"><P>
Email: <input name="email"><P>
Phone Number (Ex: 1 586 265 8744): <input name="phone"><P>
Desired Username: <input name="username"><P>
Desired Password: <input type="password" name="password"><P>
Main Domain you will be using: <input name="domain"><P>
Please Select your package:
<select name="pkg">
<option selected>Bronze
<option>Silver
<option>Gold
<option>cBronze
<option>cSilver
<option>cGold
</select>
<P>
<input type="Submit" name="Submit" value="Submit"><input type="Reset" name="Reset" value="Reset">
ENDOFTEXT
print &HtmlBot;
}
Dusty
December 19th, 2002, 13:26
I haven't gone through the second bit of code you posted, but as long as it's fine, just replace the block you wrote first with this:
$url='';
if($input{'pkg'} eq 'Bronze'){
$url='bronze.html';
}elsif($input{'pkg'} eq 'Silver'){
$url='silver.html';
}elsif($input{'pkg'} eq 'Gold'){
$url='gold.html';
}elsif($input{'pkg'} eq 'bBronze'){
$url='cbronze.html';
}elsif($input{'pkg'} eq 'cSilver'){
$url='csilver.html';
}elsif($input{'pkg'} eq 'cGold'){
$url='cgold.html';
}
if(!$url){
print '----, something went wrong !';
}else{
print '<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; url='.$url.'">
</head>
<body></body>
</html>';
}
GregT
December 19th, 2002, 15:26
thnx papa and dusty, i had it fixed b4 you posted dusty, the eq and single quotes did it. thnx anyways :)
Agum
December 20th, 2002, 05:00
everybody else already fixed your problem
but just one quick comment
why would you use the meta tag way to redirect instead of simply
print "Location: http....Bronze.html\n\n";
? it's much shorter to write and it's safer too, just in case there are some weird old browsers that don't support meta tags or something.
YUPAPA
December 20th, 2002, 08:35
Dun ask me... maybe he wants to put something on the redirection page. That was what I thought.
GregT
December 20th, 2002, 14:51
Originally posted by Agum
everybody else already fixed your problem
but just one quick comment
why would you use the meta tag way to redirect instead of simply
print "Location: http....Bronze.html\n\n";
? it's much shorter to write and it's safer too, just in case there are some weird old browsers that don't support meta tags or something.
didnt think of that... i copy and pasted, didnt write it. if they have old browsers they need to upgrade anyways :p
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.