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

Problem writing form field data to new HTML page 1

Status
Not open for further replies.

rspratt

MIS
Oct 12, 2000
4
GB
Hi there,

I am fairly new to CGI/PERL (so much so, I haven't yet worked out what the difference is). I have some moderate c, pascal and javascript experience, and am at the beginnings of learning CGI/PERL. I have a CGI problem which I am hoping someone will be kind enough to help with.

The CGI script I am using was downloaded from a library so I don't really have a good understanding of how or why it works. In short, I am having trouble taking data from an html form and getting my CGI script to write that data to another html page. I realise that this is probably quite a trivial issue for most of you great people so I do really appreciate the assistance. I will try to be as clear as I can:

Here's the HTML page that calls the script:

<html>
<head>
<title></title>
</head>
<body>
<form method=&quot;POST&quot; action=&quot; <p>Name: <input type=&quot;text&quot; name=&quot;name&quot; size=&quot;20&quot;><br>
Address: <input type=&quot;text&quot; name=&quot;address&quot; size=&quot;20&quot;><br>
<input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;Submit&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot;
name=&quot;Reset&quot;></p>
</form>
</body>
</html>


I assume the following is the CGI script code that reads the data in from my form page:

--begin--
$cl = $ENV{'CONTENT_LENGTH'};

if ($cl > 0)
{
read(STDIN, $_, $cl);
$_ .= &quot;&&quot;;
-end--


The CGI script is then supposed to write my form data to a new html page. I do not have any problem getting the new page to display, I just can't get it to display the values that were taken from my form page.

I have tried including the following code in my CGI script, in the hope that one or versions will work (but neither will):

--begin--
name = $name<br>
name = $c1{'name'}<br>
address = $address<br>
address = $c1{'address'}<br>
--end--

Unfortunately, all my new page displays is:

--begin--
name =
name =
address =
address =
--end-

Why won't the values display? I am slowly going mad, because I'm sure that it's quite simple. I have spent countless hours searching for the answer - but it has all been in vain. I have turned to you in the hope that some good person will put me out of my misery.

Many thanks,
Robert [sig][/sig]
 
My first suggestion is that as soon as you get a general understanding of CGI, start using one of the canned modules to do CGI work. My current favorite is CGI.pm. Until then, maybe a week or months, depending on how much of what you do.....

I hope you don't mind my backing up a few steps to hit the what is CGI/Perl stuff....

Perl is a programming language. You can do lots of stuff with Perl and never do any CGI and you can use it to do CGI. CGI is simply a set of rules governing how to get a process to run on a remote machine through a web server. You can write CGI code in any language as long as you follow the rules. Some languages lend themselve to following the rules, others don't. Perl makes following the rules pretty easy.

When a web page is submitted and tries to pass data to a piece of CGI on a server somewhere, the data from the browser is passed in one of two ways, one of two 'methods', GET or POST. You are using POST. So, the data from the browser is read from STDIN in the CGI code. Rather than trying to track your problem in your snippets ( they are short, missing some pieces, or maybe the code is missing the pieces), I will show a typical way of doing this trick and annotate it so you can see the steps.

Code:
# create a hash names 'FORM' of the CGI variables.
%FORM = &cgidecode;
$name = $FOPRM['name'};
$address = $FORM{'address'};

# do normal HTML page start stuff.....
print &quot;Name is $name and ADDRESS is $address<BR>\n&quot;;
# do normal HTML page end stuff.....

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 &quot;POST&quot;) 
  {
  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 values and storing them in the assoc. array %vars
foreach $pair (@pairs) 
  {
  ($key, $val) = split(/=/, $pair);
  # replace html encoding to get back to human readable stuff
  $val =~ s/\+/ /g;
  $val =~ s/%(..)/pack(&quot;C&quot;,hex($1))/eg;
  if ($key eq $last) {$vars{$key} .= &quot; &quot;.$val; }
  else { $vars{$key} = $val; }
  $last = $key;
  }
return(%vars);
}

'hope this gives some clues....good luck [sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
Like GB mentioned, CGI is not a programming language, but a protocol. You can use any language to do CGI as long as it accepts input and provides output through the necessary channels. C is good for this. But, I and everyone in this forum prefer to use Perl, primarily because it is so good at text manipulation and is so efficient code-wise.

I would suggest you get your hands on cgi-lib.pl ( which is an external library you can use to handle your CGI. You can open up this file and study how it works, or just use it.

The CGI-Lite module works very similarly to cgi-lib.pl, so you could always migrate to this if you don't want to copy cgi-lib.pl everywhere you write a new CGI script. I personally prefer this module over CGI.pm because it easily allows functional programming over object-oriented programming and is less complex to work with. Also, with CGI-Lite, you can use strict mode, whereas you cannot with cgi-lib.pl.

Still, I think cgi-lib.pl would be the best place for you to start with your CGI explorations. It can be a teaching tool in addition to a valuable library.
[sig]<p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top