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:
Make it simple

 



kevin32
Deleted

Sep 5, 2000, 11:50 AM

Post #1 of 5 (385 views)
Make it simple Can't Post

Trying to understand perl without much luck so if you could answer this question as simple as possible it would be a great help.
Basicialy all I need to know is how to get information from a form to be displayed on another html page.

Thank you


perlkid
stranger

Sep 6, 2000, 2:20 AM

Post #2 of 5 (385 views)
Re: Make it simple [In reply to] Can't Post

 ok,

First, you need to read the form. You can get this piece of code anywhere. When I started I went to cgi-resources and took it from another script.

###################################
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";
}
###################################

Now, I suppose You know that each field in your form has a name.

This name is how you retrieve the data in the cgi from the specified field.

If the name of the field is "description" then to get that information you would use $form{'description'}.

To open a file you would do this,

open(html, "/path/to/file.html");

Now it's open, the "html" is a file handle. it's the word you use to do things like print to the file, read the file, and close the file.

so to print to the file your going to do this.

print html "$form{'description'}\n";

The "\n" means newline.

Don't forget to close the file when your done with it.

close(html);

The word "html" that I used as a file handle can be replaced with any other word of your choice.

Now, You have printed to the html file. I'll assume you want to show the user what they have just put into the form.

To show them what they just submitted you need to obviousley send them there right.

Do so by using this,

print "Location: http://www.site.com/file.html\n\n"

That will redirect them to the file you printed to.

Note that the "\n\n" is manditory in this case.

Also when you want to use print"Location:\n\n"

You can't have

print "Content-type: text/html\n\n";

On the page,

print "Content-type: text/html\n";

Is ok but not

print "Content-type: text/html\n\n";

Ok?

If you don't want to show them the page the form's data was sent to then forget the

print "Location:\n\n";

And now you must have the

print "Content-type: text/html\n\n";

in the cgi.

What that does is just explains that your about to print html. If your not going to print html like you won't in a redirect, You would leave it out.

So, then with your

print "Content-type: text/html\n\n";

in your cgi (Above All Print Commands) your ready to print a message saying that there submission is appeciated bla bla bla.

Like so,

print "Thank You For Your Submission Bla Bla Bla";

If you want to put html in substitution of that message USE THIS,

print qq~<html>More Html Stuff Here</html>~;

Ok?

So lets recap.

This is the program that will print to am html file and then redirect the user there.

##########################
#!/usr/bin/perl
###################################
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";
}
###################################
open(html, "/path/to/file.html");
print html "$form{'description'}\n";
close(html);
print "Location:http://www.site.com/file.html\n\n";
exit;
#####################################

This is the program you want if you want to display a message after the submission and NOT redirect the user to the page with the form data written on it.

##########################
#!/usr/bin/perl
print "Content-type: text/html\n\n";
###################################
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";
}
###################################
open(html, "/path/to/file.html");
print html "$form{'description'}\n";
close(html);
print "thank you for you submission bla bla bla";
exit;
####################################

That's It,

Don't forget this works with data bases or any other file too, not just html files.

Hope That Helped,

perlkid

[This message has been edited by perlkid (edited 09-06-2000).]


kevin32
Deleted

Sep 6, 2000, 8:27 AM

Post #3 of 5 (385 views)
Re: Make it simple [In reply to] Can't Post

Still having trouble.
So if the form is in a file called new1.html and the description is called "user_name" and i want the name to print in file new2.html is this right?##########################
#!/usr/bin/perl
###################################
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
{
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";
}
###################################
open(html, "new1.html");
print html "$form{'user_name'}\n";
close(html);
print "Location:http://www.site.com/new2.html\n\n";
exit;
#####################################

thank for your help i'm determined to get my head around this.
Thanks



Cure
User

Sep 6, 2000, 9:38 AM

Post #4 of 5 (385 views)
Re: Make it simple [In reply to] Can't Post

use CGI();
$q = CGI->new;

$name = $q->param('username');

open NAME, '>file2.html' or die $!;
print NAME $name;
close NAME;

print $q->header;
print "Hi $name, your name has just been submitted to our file";


Cure


perlkid
stranger

Sep 6, 2000, 11:06 AM

Post #5 of 5 (385 views)
Re: Make it simple [In reply to] Can't Post

 
Hi Kevin,

Sorry Sorry Sorry,

You are doing nothing wrong, I was just soo tired last night that I forgot to tell you one thing.

when you use

open(html "new2.html");

You need a >> in there

open(html ">>new2.html");

That means to append to the file, it will keep adding to the end of the file.

> is to overwrite the file completly, and < is to read from a file.

Sorry,

Just add that and the code your using willl work.

Cure's code works too, except if you want to append to the html file, instead of the > in his open command, use >>.

Perlkid

 
 


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

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