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:
html forms and text replacement

 



codywood
Novice

Nov 30, 2004, 9:33 AM

Post #1 of 13 (2494 views)
html forms and text replacement Can't Post

Hey, I am VERY new to perl, in fact I am working on my first script. I am wanting to fix contractions by changing "dont" to "don't" etc. I am submitting a string of text via html forms. My end goal is to have a webpage with two text boxes. In the top box, is where I want the user to enter their paragraph of text, and the bottom box will contain the output of the the form (after submitting) as well as a tag saying where it was processed. (i.e. Fixed by The Contraction Corrector By Cody Wood http://www.sitelinkurl.com)

However, I cant seem to find any info on how to do this. I have the basic mechanics down on how to replace the text, but am having problems with the output. I know I will have to include a form into the script somehow. Any help would be greatly appreciated.

html form:

<html>
<head>
<title>Contractions</title>
</head>
<body>
<form action="http://164.58.187.228/cgi-bin/contract.cgi" method="POST" name="Contraction Correction"> <p><textarea rows="15" name="input" cols="63" maxlength="250">Enter text to be fixed here.</textarea></p>
<p>&nbsp;</p>
<p><textarea rows="15" name="output" cols="63">The Contraction Correction By Cody Wood http://www.comingsoon.com/</textarea></p>
<p><input type="submit" value=" &nbsp; &nbsp; submit &nbsp; &nbsp; " name="submit"><input type="reset" value="Reset" name="Reset"></p>
</form>
</body>
</html>

cgi:

#!C:\Perl\bin\perl

#===============================
# Contraction Correction
# Copyright 2004, Cody D. Wood
# Created 11/19/04
#===============================

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

if($ENV{'REQUEST_METHOD'} eq "GET"){
$my_data = $ENV{'QUERY_STRING'};
}
else {
$data_length = $ENV{'CONTENT_LENGTH'};
$bytes_read = read(STDIN, $my_data, $data_length);
}

# Load it into something we can use
@name_value_array = split(/&/, $my_data);

# Here's where we do the actual work. We cycle
# through @name_value_array to decode the name=value pairs
foreach $name_value_pair (@name_value_array) {
# Split the name=value pair in your HTML form data
($name, $value) = split(/=/, $name_value_pair);

# Now, replace '+' with ' '
$name =~ tr/+/ /;
$value =~ tr/+/ /;

# separating below here. Just have a couple for example.
$value =~ s/dont /don't /g;
$value =~ s/wont /won't /g;
$value =~ s/wouldnt /wouldn't /g;

# end of the separate

# Next, translate any hex values back into characters
$name =~ s/%(..)/pack("C",hex($1))/eg;
$value =~ s/%(..)/pack("C",hex($1))/eg;

# Finally, load the variables into an associative array
# so we can use it when we need it.
if($form_data{$name})
{
$form_data{$name} .= "\t$value";
}
else
{
$form_data{$name} = $value;
}
}

# Now this is where I am stuck, so I am just displaying it.
foreach $form_data_key (keys(%form_data)) {
print "$form_data_key = $form_data{$form_data_key}<br>";
}

#===============================
#===============================


KevinR
Veteran


Nov 30, 2004, 10:40 PM

Post #2 of 13 (2486 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post

This would be easier as a pure perl solution.

If this is your first leap into perl then it would be best to learn to use CGI.pm for your form processing and not start by learning bad and unsafe methods to parse form data sent to your perl scripts.

http://www.perldoc.com/perl5.8.4/lib/CGI.html
-------------------------------------------------


(This post was edited by KevinR on Nov 30, 2004, 10:57 PM)


davorg
Thaumaturge / Moderator

Dec 2, 2004, 1:57 AM

Post #3 of 13 (2478 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post

I don't know where you are learning Perl from, but it is giving you some very bad advice. If it is a book then please get rid of it and consider using one of the books listed in the FAQ.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


codywood
Novice

Dec 2, 2004, 6:39 AM

Post #4 of 13 (2473 views)
Re: [davorg] html forms and text replacement [In reply to] Can't Post

Actually I got the guide from about.com http://perl.about.com/library/weekly/aa070901a.htm thanks for letting me know their guide is a worthless pile of ... anyhow.. I took you guys advice and rewrote most of it, see if this is better.

#!C:\Perl\bin\perl

#===============================
# The Contraction Corrector
# Copyright 2004, Cody D. Wood
# Created 11/19/04
#===============================


use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
$CGI::POST_MAX=1024 * 100; # max 100K posts
$CGI::DISABLE_UPLOADS = 1; # no uploads

print header();
print start_html( -title=>'The Contraction Corrector',
-bgcolor=>'#CCCCCC');
print
h1('The Contraction Corrector'),
'---------By Cody Wood---------<br>',
start_form,
textarea('input','',15,63),p,
submit,
end_form,
hr,"\n";

$contract = param('input');

$contract =~ s/dont /don't /g;
$contract =~ s/wont /won't /g;
$contract =~ s/wouldnt /wouldn't /g;;

if (param) {
print $contract,p;
print "---------The Contraction Corrector---------<br>\n";
print "---------http://comingsoon----------<br>\n";

}
print end_html;


davorg
Thaumaturge / Moderator

Dec 2, 2004, 6:46 AM

Post #5 of 13 (2471 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post

That looks much more like it :)

One small suggestion. You should consider using \b escapes in your regualr expressions to prevent the fix being made when the words appear in the middle of other words.


Code
$contract =~ s/\bdont\b/don't /g; 
$contract =~ s/\bwont\b/won't /g;
$contract =~ s/\bwouldnt\b/wouldn't /g;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


codywood
Novice

Dec 2, 2004, 7:12 AM

Post #6 of 13 (2469 views)
Re: [davorg] html forms and text replacement [In reply to] Can't Post

nice. I didnt know that - I will make the changes. Also, just playing with this script and its possible uses, how would I make it randomize a replacement? like if I wanted it to find a word, and then replace it with one of 3 or 4 words?


davorg
Thaumaturge / Moderator

Dec 2, 2004, 7:19 AM

Post #7 of 13 (2467 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $text = 'this is some text';

my @replace = qw(food money water);

$text =~ s/\btext\b/$replace[rand @replace]/g;

print "$text\n";


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


codywood
Novice

Dec 2, 2004, 7:33 AM

Post #8 of 13 (2465 views)
Re: [davorg] html forms and text replacement [In reply to] Can't Post

Wow, this site is awesome, thanks for all your advice davorg. You've saved me tons of time and I've learned more in 2 days than what I had learned in 2 weeks on the net.

I've got some ideas to what I'm gonna put all this great info to work on. I'll follow up with a link to the finished product when I'm done.

Thanks again and keep up the good work!

-Cody

OH! one last thing, is there a way to send my output to another text box instead of just the ugly print below the input box I currently have?


davorg
Thaumaturge / Moderator

Dec 2, 2004, 7:56 AM

Post #9 of 13 (2462 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post


In Reply To
OH! one last thing, is there a way to send my output to another text box instead of just the ugly print below the input box I currently have?


Well you have all the formatting possibilities of HTML open to you. And that can make things look a lot different. Especially if you use CSS.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


codywood
Novice

Dec 2, 2004, 8:04 AM

Post #10 of 13 (2461 views)
Re: [davorg] html forms and text replacement [In reply to] Can't Post

Playing around with the random text replacement, its replacing every hit on the replace to the same string it pulls from the random. Is there a way to random on every occurrance of the word?


davorg
Thaumaturge / Moderator

Dec 2, 2004, 8:09 AM

Post #11 of 13 (2459 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post

Yeah, sorry. I meant to put that in.


Code
$text =~ s/\btext\b/$replace[rand @replace]/eg;


Add the /e option to the substitution operator.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


codywood
Novice

Dec 6, 2004, 11:59 AM

Post #12 of 13 (2447 views)
Re: [davorg] html forms and text replacement [In reply to] Can't Post

Ok, here is the finished product. I took the idea and ran with it. let me know what you think.

http://164.58.187.228/cgi-bin/flame.cgi


KevinR
Veteran


Dec 6, 2004, 1:00 PM

Post #13 of 13 (2443 views)
Re: [codywood] html forms and text replacement [In reply to] Can't Post

hmmm... I feel like we were used. Unimpressed
-------------------------------------------------

 
 


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

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