Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this "ilegal" in perl

Status
Not open for further replies.

ghayman73

Technical User
Jul 3, 2002
55
FR
I am trying to send a value to a page using this script:

Code:
#!/usr/bin/perl
read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$temp);
foreach $item(@pairs)
 {
  ($key,$content)=split(/=/,$item,2);
  $content=~tr/+/ /;
  $content=~s/%(..)/pack("c",hex($1))/ge;
  $fields{$key}=$content;
 }                                       
print "Content-type: text/html\n\n"; 
print &quot;<HTML>\n&quot;;
print &quot;<BODY BGCOLOR=#FFFFFF>\n&quot;;
print &quot;<CENTER>\n&quot;;
print &quot;$fields{option1|verse}<BR>\n&quot;;
print &quot;$fields{option1}<BR>\n&quot;;
print &quot;</CENTER>\n&quot;;
print &quot;</BODY></HTML>&quot;;

The problem is I cant get the field {option1|verse} to work.
Is this because it is ilegal in the pearl language or is my script to baisic.
I need to use this as a field because its for a shopping cart and they need it to be passed to them using this field.
I have tried just {option1} and that works fine but when i add the | nothing happens.
Does anyone know a way around this or if I use a diffrent handling script will it work.

Thank you
Grant

 
Code:
#!perl 
%hash = ('vw'=>'blue','gm'=>'red','ford'=>'green');
print &quot;VW: $hash{vw}\n&quot;;
print &quot;VW|GM: $hash{vw|gm}\n&quot;;

%hash = ('vw'=>'blue','gm'=>'red','ford'=>'green','vw|gm'=>'violet');
print &quot;VW|GM: $hash{'vw|gm'}\n&quot;;

=header
Your syntax is valid, but I don't think it will do what you 
want it to.

Your appear to be trying to print the value associated
the key 'option1' if it exists.  Otherwise, print the value
associated with the key 'verse'.

If that is what you are trying to do, this will work.
if ($fields{option1})
    {
    print &quot;$fields{option1}<br>\n&quot;;
    }
elsif ($fields{verse})
    {
    print  &quot;$fields{verse}<br>\n&quot;;
    }

OR, like this,
$print_this = $fields{option1} ? $fields{option1} : $fields{verse};
print &quot;$print_this<BR>\n&quot;;
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
If you need one value or the other (but not both values), you could try:

Code:
my $result = $fields{option1} ? $fields{option1} : $fields{verse};
print &quot;$result<BR>\n&quot;;

This is called the ternary operator(?:), the basic model looks like this:

Code:
EXPRESSION ? IF_TRUE : IF_FALSE;

This will set $result based on whether $fields{option1} is true(set) or not. If it's true, then $result gets $fields{option1}, if $fields{option1} is false(null or not set), $result gets $fields{verse}.

I'm not certain, but this is what it looks like you're trying to do. [wink] If both 'option1' and 'verse' are always going to be set or you need both at the same time, you'll need to try something else... Notorious P.I.G.
 
Thank you both for your time.

I need both because they are one field.

The overall thing im trying to do is to create a page using cgi(perl) only because ive used this before to do simlar things. The page in going to have a form with a text field in, the text field has to be named &quot;option1|verse&quot; because thats the only name the shopping cart will except for this field, in all there is three fields (option2|peronalizedtitle) and (option3|ending).
The form is basicly an oreder form with a submit button.
Since my post I have found a way that will recognise &quot;option1|verse&quot; as a field using this:
Code:
#!/usr/bin/perl
use CGI;
#create an instance of the CGI object 
$cgiobject = new CGI;

#grab the values submitted by the user
$versetitle=$cgiobject->param(&quot;option1|Verse Title&quot;);


#output HTML header to web browser
print $cgiobject->header;

but i cannot get it to insert the form previously I have just enterd the HTML from dreamwever into the cgi script and it worked fine (the script was from a guestbook script i played around with) but when i do the same with this I get an error message (misconfiguration).
Ive been reading all day and have found that I made need to use
print &quot;<HTML><HEAD>&quot;;
print &quot;<TITLE>A website</TITLE>&quot;;
print &quot;</HEAD><BODY BGCOLOR=\&quot;#FFFFDE\&quot;>&quot;;
print &quot;<P ALIGN=\&quot;CENTER\&quot;>This page was created<BR>&quot;;
print &quot;</BODY></HTML>&quot;;

Is this true or is there a better way
Im begining to relise how complex perl is and they say it is one of the easier languages to learn(I wish).
any I hope this makes a bit more sense to what I am trying to do.

One more thing, I am hoping to fill in the vaules of the fields in the link to the cgi script somethin like this:

greatest mom in the world

do you think this is a good idea?

Thank you again
 
Sorry
my head has gone totaly, the code above doent work ive been trying so many ways and i thought i had it but alas it was working the wrong way it was passing the value from the field &quot;option1|Verse Tiltle&quot; not to it.

grant
 
Hi
Ive gone away and had a re-think and this is what im am now trying:
Code:
#!/usr/bin/perl
&getdata;

sub getdata{

# Read in text
  if ($ENV{'REQUEST_METHOD'} eq &quot;GET&quot;) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq &quot;POST&quot;) {
    for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
      $in .= getc;
    }
  } 

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Convert %XX from hex numbers to alphanumeric
    $in[$i] =~ s/%(..)/pack(&quot;c&quot;,hex($1))/ge;

    # Split into key and value.
    $loc = index($in[$i],&quot;=&quot;);
    $key = substr($in[$i],0,$loc);
    $val = substr($in[$i],$loc+1);
    $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
    }
}

if ($in{id}==001)
 { 
   $in{'title'}=&quot;18 Birthday Wishes&quot;; 
 }
 { 
   $in{'ptitle'}=&quot;Happy 18 bithday wishes&quot;; 
 }

if ($in{id}==002)
 { 
   $in{title}=&quot;Congratulations - Baby Daughter&quot;;  
 }

 { 
   $in{ptitle}=&quot;to my beatifull daughter&quot;; 
 }
[\code]

so now I have it that the link contains ?id=001 (or another number)as you can see if 001 is in the link im am trying to declare the 2 values 'title' and 'ptitle'so i can then use them in my form.
however when i run the script it returns only the first word in the form e.g. instead of 'to my bea....' I get just 'to' I have tried to play with the script but with no joy.

p.s. i'm sure there is a way of declaring to values at a time but I cant seem to get that right either.

thnks again 
Grant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top