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!

Still The same problem with POST method

Status
Not open for further replies.

TyzA

Programmer
Jan 7, 2002
86
BE
Hi all,

About a week ago I post this on this forum.

"I'm new to CGI and i have a problem with the post method.
In my HTML I have a small form that uses the POST method.
The problem I have, is that in my CGI (Perl) script the string i receive from STDIN is empty. I've tried different ways to do it but it never works.
I use: read (STDIN, $input, $ENV{CONTENT_LENGTH}).
I'm able to print the content length, but $input is always empty. It prints nothing"

An export, called goBoating, post this on my thread. (I really hope you are reading this goBoating):

"
#!/usr/local/bin/perl
%FORM = &cgidecode;

# if the html form contained a 'name' input,
$name = $FORM{'name'};

sub cgidecode
{
local(%vars, $val, $key, $last, $buffer, $pair, @pairs);

# Checking the form method (GET or POST) used in the
# HTML code. POST method sends data to standard input,
# but GET adds it to the URL and stores it in
# QUERY_STRING.
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
else
{
$buffer = $ENV{'QUERY_STRING'};
}

# Splitting up the data fields and store
# in array @pairs, they are seperated with &
@pairs = split(/&/, $buffer);

# Splitting the variable names and the values
# and storing them in the assoc. array %vars
foreach $pair (@pairs)
{
($key, $val) = split(/=/, $pair);
$val =~ s/\+/ /g;
$val =~ s/%(..)/pack("C",hex($1))/eg;
if ($key eq $last) {$vars{$key} .= " ".$val; }
else { $vars{$key} = $val; }
$last = $key;
}
return(%vars);
}
"

I've tried to do this, but my STDIN is still empty, I think.
When I try to print $buffer, after STDIN is read in, it prints nothing.
Maybe something is wrong with my HTML form. Here it is:

&quot;<form action=&quot;../../cgi-bin/contact.cgi&quot; method=&quot;POST&quot;>
<input type=&quot;text&quot; name=&quot;email&quot; maxlength=&quot;50&quot;><br>
<input type=&quot;text&quot; name=&quot;onderwerp&quot; maxlength=&quot;150&quot;><br>
<textarea cols=&quot;25&quot; rows=&quot;10&quot; name=&quot;commentaar&quot;> </textarea><br>
<input type=&quot;submit&quot; name=&quot;zend&quot; value=&quot;Zend&quot;>
</form>&quot;

I realy hope you guys can help me.

Thanks a lot

Best regards,

Tijs Programming is like sex: one mistake and you have to support it for the rest of your life.
 
You could use CGI.pm to do all the dirty work of reading in parameters for you. I tested your homespun solution against a simulated post:
Code:
export REQUEST_METHOD=POST
export CONTENT_LENGTH=21
echo 'name=john&surname=doe' | your.cgi
With a little mod to print all parameters found:
Code:
print map { &quot;$_ => $FORM{$_}\n&quot; } keys %FORM;
This worked fine. Possible reasons why your $name variable is not getting set:
1) The form you are submitting does not contain an [tt]<input name=&quot;name&quot;>[/tt] element.
2) You may have some case issues.
Hope some of this helps. Cheers, Neil


 
The code you posted looks fine to me. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top