×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

problems reading variables

problems reading variables

problems reading variables

(OP)
Hi,

I’m busy with a script that creates an html page with a number of links to the same script, a link looks like this:

<a href="consult_exp.pl?add=show&exp_nr=12"><b>12</b></a>

What I don’t understand is that the variables ‘add’ and ‘exp_nr’ are not available in the script. I checked it by trying to print them in the browser which confirmed this as they didn’t show any value.

Reading the variables works fine when using Post but in this case I have to send the variables in the way shown above.

Somebody can tell me what I’m doing wrong?

Jett



Oh, a short one. How to increment the value of a variable with 1.
$var=$var++;
or $var=$var+1;
both don’t work?

RE: problems reading variables

The short one:

$var = $var++;

Hmm... You're not really using this in the way nature intended, but let's have a look at why it's not working first.

Say that $var is equal to 1 at first, this is executed in the following order:

1 $var is set to the current value of $var (1)
2 The ++ is evaluated (giving 2) an the value thrown away.

This is because the ++ operator used as a postfix (after the variable) will get evaluated *after* anything else.

$var = ++$var would work but it's better to just say $var++ and leave it at that. $var++ increments the value of $var by one with no trouble.

The second one $v = $v + 1; that should work just fine.... Look at your code again.

The web stuff? Not a clue I'm afraid, all a bit new-fangled for me but someone else will know (goBoating?)

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site
Making mistakes, so you don't have to. <grin>

RE: problems reading variables

(OP)
Mike, thank you for helping with the increment question. I must have done something wrong as $v=$v+1; does work. It is used to increment a value read from a data file. I didn’t try this yet, maybe the problem is caused somewhere else.

Question 1:

Underneath is a stripped version of the script that doesn’t read the variables. First it creates a new page with a link based on a set of variables. Then after clicking the link it should go to ‘sub conshow’ but it doesn’t as the variables aren’t read?

#!/usr/bin/perl

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;
        
        
        $input{$name} = $value;
}

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

if ($input{'add'} eq "show") {
&conshow;    
}

$add='"';
$b=12;
$cons='consult.pl?add=show&exp_nr=';
      $addtohtml .="Reference: <a href=$add$cons$b$add>$b</a>";
print <<EOF;
$addtohtml
EOF
exit;

sub conshow {
print "$input{'add'}, $input{'exp_nr'}";
exit;
}


In the page the link looks like this:
<a href="consult.pl?add=show&exp_nr=12">12</a>

Please somebody help!

Jett

RE: problems reading variables

(OP)
I'm still not finished with this and I think the problem is somewhere else.

I made a simple form which is sent using 'get' in order to create the same sort of url:

consult.pl?add=show&exp_nr=12

This isn't working either!

Can anybody tell me how to use this way of sending variables to a script (how to read it into the script) as obviously I'm doing something wrong?

,Jett

RE: problems reading variables

Hello Jett,
I think you are very close.  As you probably know, there are two form methods, GET and POST.  When you use a URI like,

consult.pl?add=show&exp_nr=12

to fire your code, you are acting like a GET action.  The syntax for decoding the $ENV vars for a 'GET' is,

$buffer = $ENV{'QUERY_STRING'};

You are trying to decode it like it came from a POST.  I think you can get your code to work if you make it a little more flexible by addressing GET and POST.  If I'm not using CGI.pm for some reason, I generally 'require' a file that contains a URI decoding sub routine that will handle GET and POST submissions.......like..

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

'hope this helps.....





keep the rudder amid ship and beware the odd typo

RE: problems reading variables

(OP)
I needed this! I already started digging in all sorts of scripts to find the solution as it had to do with using 'get' instead ofr 'post' but I didn't yet manage to find a script that used 'get' to see the difference.

Thank you so much goBoating. I'm trying to create some script and to my own surprise it is working out quite well untill I run into this little obstacle that seemed nowhere near the most difficult part of the script!

Best regards,

Jett

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close