Salutation
Name
Surname
Email
However, when I get the result the order of the fields gets distorted. I get it in the following order:
Surname
Email
Name
Salutation
How can I get cgi-lib.pl to give me the results in the same order as the HTML form?
The code I use is as follows:
#!/usr/bin/perl
require "cgi-lib.pl";
&ReadParse;
print "Content-type: text/html\n\n";
print <<"PrintTag";
<HTML>
<HEAD>
<TITLE>Testing Form Input</TITLE>
</HEAD>
<BODY>
<TABLE>
PrintTag
foreach $key (keys(%in))
{
print <<"PrintTag";
<TR>
<TD>$key</TD>
<TD>$in{$key}</TD>
</TR>
PrintTag
}
print <<"PrintTag";
</TABLE>
</BODY>
</HTML>
PrintTag
Yes, I am from the old school and still use cgi-lib.pl. So please excuse me.
So we need to do it in some other more generic way.
If you can help me further with this, please do. I'll appreciate it.
#!/usr/bin/perl
require "cgi-lib.pl";
&ReadParse;
s/=[^=]*$// for @in;
print "Content-type: text/html\n\n";
print <<"PrintTag";
<HTML>
<HEAD>
<TITLE>Testing Form Input</TITLE>
</HEAD>
<BODY>
<TABLE>
PrintTag
foreach $key (@in)
{
print <<"PrintTag";
<TR>
<TD>$key</TD>
<TD>$in{$key}</TD>
</TR>
PrintTag
}
print <<"PrintTag";
</TABLE>
</BODY>
</HTML>
PrintTag
cgi-lib is so old you should not be using it. It is written for perl 4 and there is no gaurantee it will continue to work with more modern perl versions.
I'll try it out later and keep you posted in this forum. Wont be for a few hours yet.
Meanwhile, can you please tell me what this extra line
s/=[^=]*$// for @in;
does? What does it mean in plain English?
I am in the process of migrating to CGI.pm, but finding it rather difficult. All books using CGI.pm use strict CGI. And that is very unforgiving. So I have stuck to cgi-lib.pl.
Any suggestions as to the best websites to visit or the books to read for such migration? I write very elementary scripts as I do web design only as a hobby and not as a day job.
When you call the ReadParse() function, the cgi-lib.pm module creates an array @in and a hash %in. @in is simply the name value pairs in one string:
name=value
name=value
etc
etc
and the hash is key/value pairs:
keys values
------------
name value
name value
etc
etc
the array has the form fields in the same order as the form, which is what you wanted. Now this bit of code:
s/=[^=]*$// for @in;
which is the short form of:
for (@in) {
s/=[^=]*$//;
}
removes the "=value" part from the "name=value" strings in the array leaving you with just the names of the form fields. The names are the same as the keys in %in. So you can use that to print out the form fields in original order.
<INPUT TYPE="Hidden" NAME="variable_order" value="Salutation,Name,Surname,Email"
foreach $key (split /,/, $in{variable_order}){...}