Forum Moderators: coopster & phranque

Message Too Old, No Replies

newbie question - help with multiplying form inputs

Help would be really appreciated

         

roly

7:20 am on Jan 11, 2001 (gmt 0)

10+ Year Member



Hi

This is my first attempt at making a perl script of my own and hence would really appreciate any help with it. I have a form
[domain2trade.com...] which has questions with between one and five different answers for each question. These answers are given by clicking a radio button. The radio buttons have each been given a numerical value but not all will be selected and obviously it is unknown which ones they will bew.

I want to multiply an initial value by all the chosen radio buttons given numerical values by each other and print the result.

I've had a go but it doesn't work - Server 500 error - and if anyone can see where I am going wrong it would be REALLY APPRECIATED!

Thanks in advance

#!/usr/bin/perl

##########################################################
#This script is copyright 2001 of Roland Satchell
#No unauthourised use of this script
#contact us at admin@domain2trade.com
##########################################################

#initial value

$initial="3000000";

#currency exchange rate
$dollar="1.6";
$pound="1";
$frank="10.38";
$euro="1.49";
$mark="3.1";
$rupee="69.34";
$lira="3063";

#Define currency form inputs
$currency="dollar";
$currency="pond";
$currency="frank";
$currency="euro";
$currency="mark";
$currency="rupee";
$currency="lira";

#
#extension factor
$com="1";
$net="0.25";
$org="0.11";
$couk="0.18";
$extoth="0.1";

#define extension
$extension="com";
$extension="net";
$extension="couk";
$extension="org";
$extension="extoth";

#
#length factor
$lengfour="1";
$lengnine="0.7";
$lengten="0.5";
$lengfourt="0.3";

#define length
$length="lengfour";
$length="lengnine";
$length="lengten";
$length="lengfourt";

#
#hyphen factor
$hyphen="1";
$hyphen1="0.4";
$hyphentwo="0.15";
$hyphenthree="0.1";

#define hyphen
$hyph="hyphen";
$hyph="hyphen1";
$hyph="hyphentwo";
$hyph="hyphenthree";

#
#abreviation factor
$abrevno="1";
$abrevyes="0.35";
$abrevyespoor="0.18";

#define abreviation
$abrev="abrevno";
$abrev="abrevyes";
$abrev="abrevyespoor";

#
#spelling factor
$speone="1";
$spetwo="0.3";
$spethree="0.15";
$spefour="0.1";

#
#define spelling
$spell="speone";
$spell="spetwo";
$spell="spethree";
$spell="spefour";

#
#words factor
$oneword="1";
$twoword="0.65";
$threeword="0.35";
$fourword="0.22";
$fiveword="0.18";

#define words
$word="oneword";
$word="twoword";
$word="threeword";
$word="fourword";
$fiveword="fiveword";

#
#Commercial demand factor
$demone="1.4";
$demtwo="1.2";
$demthree="1.0";
$demfour="0.8";
$demfive="0.6";

#define demand
$demand="demone";
$demand="demtwo";
$demand="demthree";
$demand="demfour";
$demand="demfive";

#
#genericity factor
$genone="1";
$gentwo="0.5";
$genthree="0.13";

#define genericity
$generic="genone";
$generic="gentwo";
$generic="genthree";

#
#memorability
$memone="1";
$memtwo="0.7";
$memthree="0.5";

#define memorability
$mem="memone";
$mem="memtwo";
$mem="memthree";

#
#common word factor
$commone="1";
$commtwo="0.9";
$commthree="0.8";
$commfour="0.6";
$commfive="0.4";

#Define common word
$common="commone";;
$common="commtwo";
$common="commthree";
$common="commfour";
$common="commfive";

#
#Numbers contained in domain
$numone="0.35"
$numtwo="0.10"

#calculate sub1
$appraisetotal1="$initial*$currency*$extension*$length*$hyph*$abrev*$spell"

#calculate sub2
$appraisetotal2="$mem*$common*$word*$demand*$generic"

#calculate grand total
$appraisetotal="$appraisetotal1+$appraisetotal2"

#print results
print "<CENTER><H4>Your Domain Name $FORM{'domain'}</H4></CENTER>\n";
print "<CENTER><H4><Has an estimated Domain Value of $currency $appraisetotal</H4></CENTER>\n";
print "<p>\n";

oilman

7:32 am on Jan 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey roly - welcome to WebmasterWorld.

I am by no means a perl expert but I'm sure that you will need to go with arrays for defining your variables and using data from the input form to specify which values you need out the array.

For example:
#Define currency form inputs
$currency="dollar";
$currency="pond";
$currency="frank";
$currency="euro";
$currency="mark";
$currency="rupee";
$currency="lira";

would be:
@currency=(dollar pond frank euro mark rupee lira);

and you would build the drop down in your form like so:
<select size="1" name="curr_selection">
<option value="0">dollar</option>
<option value="1">pond</option>
<option value="2">frank</option>
<option value="3">euro</option>
<option value="4">mark</option>
<option value="5">rupee</option>
<option value="6">lira</option>
</select>

and then in your equation you would use:
$appraisetotal1=$currency[$cur_select]*.....

Hang tight and our resident perl gurus will be along shortly to provide an answer that is far more useful than mine and will probably correct my methods as well;)

littleman

8:01 am on Jan 11, 2001 (gmt 0)



There are a couple of reasons the script is giving you a 500 error.
You will probably have to add

print "Content-type: text/html\n\n";
to the script.
You have a couple of semicolons missing. The first one is at line 167, After that, you'll need to remove the quotation marks from these two lines:

calculate sub1
$appraisetotal1="$initial*$currency*$extension*$length*$hyph*$abrev*$spell";

#calculate sub2
$appraisetotal2="$mem*$common*$word*$demand*$generic";
You will also need to have a form parcing sub routine. Here is one:

sub Parse_Form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

if ($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/, $ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}

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

$value =~s/<!--(.¦\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
}


Put '&Parce_Form;' at the top of the script to parse the string, than you assign the value like this:

$url = $formdata{'url'};
$user = $formdata{'user'};
$proxy = $formdata{'proxy'};
$use = $formdata{'use'};
This of course has to match the value of the html form.

littleman

8:17 am on Jan 11, 2001 (gmt 0)



Could you tell us what you are trying to do with the define parts of the script?
For instance what are you trying to do in these parts?

#define words
$word="oneword";
$word="twoword";
$word="threeword";
$word="fourword";
$fiveword="fiveword";
#define spelling
$spell="speone";
$spell="spetwo";
$spell="spethree";
$spell="spefour";

You probably would want to use an array like Oil said. BTW, I'm no perl guru. Air and Brett are quite good, I have a feeling Dave may be too, though he is too modest to tell anyone.

DaveAtIFG

11:14 am on Jan 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LM overestimates me, I can adapt a few code snippets, and occasionally get something to run... I didn't see any problems beyond the missing semis but wanted to offer a suggestion. PerlBuilder at www.solutionsoft.com/perl.htm makes developement much easier then debugging on a remote host, catches these syntax errors, has a decent editor and debugger... and a trial version. If you don't have a better tool, you may find it useful.

roly

11:03 pm on Jan 11, 2001 (gmt 0)

10+ Year Member



Thanks very much to all of you it's really appreciated. I will try and sort it out now, I'll call back and let you know when it's done.
I'm a mechanical engineering student so the web is only a bit of a hobby (although i'd prefer to make a living from it if possible). But I think I'll be able to get my head round this perl programming (with time and perseverence)should allow me to take my site up a level or too (i've only ever been able to use premade scripts in the past which is a bit limiting.

So thanks once again and hopefully speak to you again.

P.S. littleman asked:

Could you tell us what you are trying to do with the define parts of the script?
For instance what are you trying to do in these parts?
#define words
$word="oneword";
$word="twoword";
$word="threeword";
$word="fourword";
$fiveword="fiveword";
#define spelling
$spell="speone";
$spell="spetwo";
$spell="spethree";
$spell="spefour";

I wanted to multiply all the chosen form inputs only and figured that if i did something like this it would let me say
$spell*$word*..... rather than $speone*threeword*... etc as i didn't know which ones would be chosen. I was making it up as i went along really and guessing so theres no supprise it was wrong.

The editor/debugger looks good and will help enourmously, thanks again.

Roly

roly

5:09 am on Jan 12, 2001 (gmt 0)

10+ Year Member



just one quick other thing when using the arrays e.g.

#abreviation array
@tld=(1=Perfect 2=VeryGood 3=Good 4=Poor);

If my first option label on the form was:
1 = Perfect

would i write the array as:
@tld=(1_=_Perfect 2=VeryGood 3=Good 4=Poor);

Hope you can help. P.S. downloaded perl builder and that is a major help - thanks

littleman

6:22 am on Jan 12, 2001 (gmt 0)



In that example you would want to use a hash instead. Go to [cgi101.com ] and scroll down about 2/3 of the way. There you will find a nice explanation on how to write a hash and how to use it. BTW, that is an excellent place to start when you are learning perl.

Also, if you are going to be writing a lot of code , you should download perl onto your computer. Activestate has a download of perl with a bunch of goodies. Here is the
download page [activestate.com]. It also has documentation in html format.

roly

6:26 pm on Jan 13, 2001 (gmt 0)

10+ Year Member



thanks for the tips, I'll have a go at the hash array now. I've also got the perl builder and activestate perl which is really good. I didn't realise i could emulate a unix server like that so testing should be a lot easier (and save a fortune on phone bills).

p.s. sorry about the duplicate posts I keep hitting new topic as opposed to post reply