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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems reading variables

Status
Not open for further replies.

jett

Technical User
Sep 13, 2000
22
0
0
US
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=&quot;consult_exp.pl?add=show&exp_nr=12&quot;><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?
[sig][/sig]
 
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?) [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
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(&quot;C&quot;, hex($1))/eg;


$input{$name} = $value;
}

print &quot;Content-type: text/html\n\n&quot;;

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

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

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


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

Please somebody help!

Jett [sig][/sig]
 
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 [sig][/sig]
 
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 &quot;POST&quot;)
[tab]{
[tab]read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
[tab]}
else
[tab]{
[tab]$buffer = $ENV{'QUERY_STRING'};
[tab]}

'hope this helps.....


[sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
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 [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top