CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Creating a %input from a web form.

 



brian.hayes
User

Jan 9, 2000, 2:16 PM

Post #1 of 13 (5290 views)
Creating a %input from a web form. Can't Post

FORM method="GET" action=""
input type="text" name="name" size="20"
input type="text" name="name" size="20"
input type="text" name="name" size="20"
/FORM

My objective is to have a list of text boxes with the same name to create a %hash of values. This is so I can reference them later within the script.


my $input = $ENV{'QUERY_STRING'};
# name=jo&name=John&name=jane

my (@var)=split(/\&/,$input);
#@var = ('name=jo','name=john','name=jane')
foreach $var(@var){
my ($key,$value)=split(/\=/,$var);
%INPUT{$key}=$value;
}

so if refrenced it would be like:
foreach $value(%INPUT{'name'});
print "$value";
#should output 'jo john jane'
}
or
print %INPUT{"name[2]"};
#should give me jane.

Please let me know if there is an easier way of doing this or if I have the right Idea.


Borderline
Deleted

Jan 9, 2000, 2:32 PM

Post #2 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Why would you want all the fields the same name?
This will not work. Hashes can not have keys of the same value.
Example:
%INPUT = ('name'=>'scott','name'=>'brian');
for (keys %INPUT) {print}

This will print name once. Because it eliminated the duplicate key. Also if you
want to process the input from a form your self you will have to UN-encode it.
I highly recommend using
CGI.pm. It is
the Standard for CGI programming in Perl. There is also excellent documentation
for it.

Hope this helps
Scott


Borderline
Deleted

Jan 9, 2000, 2:38 PM

Post #3 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Hey,

Sorry I re-read your post and noticed you were giving unique keys.
But you were still not UN-encoding the data. I would still recommend you look at using CGI.pm. It does all this for you and gives you access to tuns of useful code....

Scott


brian.hayes
User

Jan 9, 2000, 2:53 PM

Post #4 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

The reason for form feilds with the same name is that I do not know of a better way to read in a file and create the form feild based on them. Also I forgot I am using CGI.pm.

basically I wanted to create a web page that would read in a file and creat text boxes based on the information. Then allow for the changes to be saved.

Here is a more realistice code example.


use CGI;
my $c = new CGI;
print $c->header;

if(defined ($c->param('admin')) && $c->param('admin') eq 'admin')){

open(FILE,$file); $file is the location of file.
@lines = <FILE>;
foreach $lines(@lines){
print qq~<input type="text" name="org" size="20" value="$lines">~;# Note $lines.
}

close(FILE);

print qq~<input type="text" name="newline" size="20">~;
## Rest of form code here...
}
undef $c;

IF the file had two lines in it it would create a text box with the values of each line , but with the same name and once completed the loop it would create a new text box that was empty with a diferent name. This is where I have always gotten stuck. How to get the information that has change back in to the CGI script in a way that the file could be updated. I know I am doing something wrong just based on the way you coded in your example. with =>. If you have any suggestion's out side of read a book please let me know(us).


Borderline
Deleted

Jan 9, 2000, 3:17 PM

Post #5 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Try this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use CGI;

$c = new CGI;
if(defined ($c->param('admin')) && $c->param('admin') eq 'admin')){
open FILE,$file or die "Could not open $file: $!";
$i = 1;
while (<FILE> ) {
print qq~<input type="text" name="org$i" size="20" value="$_">~;
$i++;
}
print "<INPUT type=hidden name=num value=$i>";
close FILE;
</pre><HR></BLOCKQUOTE>

And to retrive the values try this:<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


for (1..$c->param('num')) {
print $c->param("org$_"),"\n";
}</pre><HR></BLOCKQUOTE>
This should print all the values submited. Ofcoarse you could do other things with them like push them into an array etc...

Let me know if this helped,
Scott


brian.hayes
User

Jan 9, 2000, 3:53 PM

Post #6 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Perfect!!!

I asume that $_ is the numeric value of the
1..max value in the param('num'). Now all I need to do is split values within a file assign a unic id via i++"incrementing" and ad for loops based on the param name foreach of the form vields that where incrimented with the i++ and the rejointhem once the changes were made and rewrite them to file.. Which I know how to do now..This was the missing peace.. Thanks...

for (1..$c->param('num')) {
print $c->param("org$_"),"\n";
}

Brian Hayes



brian.hayes
User

Jan 9, 2000, 4:02 PM

Post #7 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Let me know if you want me to post the completed working code for everyone to view.
By the way is there any good editors that are specific to perl programing?


Borderline
Deleted

Jan 9, 2000, 4:03 PM

Post #8 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Your welcome!

Sorry I should have explained the $_ a little bit better.
$_ is the default for a lot of things. In the two cases I gave you it was defaultly set as the value for each iteration of the loops.
The alternative is to set it yourself
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


for $num(1..$c->param('num')) {
...
}</pre><HR></BLOCKQUOTE>
or for the while loop
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while ($line=<FILE> ) {
...
}</pre><HR></BLOCKQUOTE>
Notice I did not set the values in the script example I gave you and I used $_ for the value.

Let me know if this does not make sence and I will clarify
Scott


Borderline
Deleted

Jan 9, 2000, 4:04 PM

Post #9 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Feel free to post any code you do here Smile

As for editors what operating system are you on?

Scott


brian.hayes
User

Jan 9, 2000, 4:12 PM

Post #10 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Well??
Linux 6.x, window 95/98/NT4.0, I have quite a collection. I have only been doing perl for a month and I already have writen some code based on the reading of others and credit is always given to where it belongs...Just for information only I have been doing the Vhosting with Linux though for some time know, and I have just haven't taken the time to automate anything. Thats where perl is comming in. I design corprate networks and seeing all of the options I choose perl as my back end language. I just need to learn my own.(spelling).:=)


Borderline
Deleted

Jan 9, 2000, 4:20 PM

Post #11 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Well when I was on Windows I used EditPlus. It is Awsome and the only thing I miss about windows. On linux I am using <A HREF="ftp://ftp.fnal.gov/pub/nedit" TARGET=_blank>Nedit</A>. It is OK but EditPlus is better. Smile

Scott


brian.hayes
User

Jan 9, 2000, 6:03 PM

Post #12 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Well here it is. It work via GET or POST method..


#!/usr/perl -w
use CGI;
my $c = new CGI;
print $c->header;

my $script = $ENV{'SCRIPT_FILENAME'};
my $db = "$script.db";

if(!defined $c->param('save')){
&default_page;
} elsif (defined $c->param('num') && $c->param('save') eq "save"){
&save_file;
} else { exit; }


sub default_page{
my $i = 1;
open FILE,$db or die "Could not open $db: $!";
print qq~<form method="GET" action="">\n~;
while (<FILE> ) {
print qq~<p><input type="text" name="org$i" size="20" value="$_"></p>~;
$i++;
}
print qq~<p><input type="text" name="org$i" size="20" value=""></p>~;
print "<INPUT type=hidden name=num value=$i>";
print "<INPUT type=hidden name=save value=save>";
print qq~<input type="submit" value="Update now">\n~;
print qq~</form>\n~;
close FILE;
undef $c;
}


sub save_file{
open FILE, ">$db" or die "Could not open $db: $!";
for (1..$c->param('num')) {
if ($c->param("org$_") ne ""){
print FILE $c->param("org$_"),"\n";
print "<p><b>Saving....</b>";
print $c->param("org$_");
print "</p>\n";
}
}
close FILE;
print "<a href=\"";
print $c->referer;
print "\"\>Back</a>\n";
undef $c;
}


Thanks again for the help..

Brian Hayes


brian.hayes
User

Jan 9, 2000, 6:13 PM

Post #13 of 13 (5290 views)
Re: Creating a %input from a web form. [In reply to] Can't Post

Also the explorer like links on the top of this page are good. I would like to send you some new gif's that have a bit more detail and are sized to file a browser line better. To who do I send them too?? For evaluation..Or I can point you to a page on my site to download them. Either way let me know..

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives