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

Form handler sometimes works, sometimes doesn't

Status
Not open for further replies.

megp

Programmer
Aug 25, 2004
31
US
I use a CGI script to process quote requests submitted from my website at EMPE Media. When I've made test submissions, they've gone through 9/10 times. I had a couple of other people submit requests that I know haven't come through. What am I missing? Why does it work sometimes and not others?

Thanks for the help,
Meghan

Code:
#!/usr/bin/perl

#Run subroutine to turn form data into an array named %in
require "cgi-lib.pl";
&ReadParse;

#Define variables identifying various email paths
$mpmail = "mp\@empemedia.com";
$mp = "EMPE Media";

#Define variable identifying path to sendmail program
$mailprog = '/usr/lib/sendmail';



#Open sendmail program to send message to inquirer
open (MAIL, "|$mailprog -t") || die "Can't open mail program\n";

#Print message headers (To, Reply-To, From, Subject)
print MAIL "To: $in{'email'}\n";
print MAIL "Reply-To: $mpmail\n";
print MAIL "From: $mp\n";
print MAIL "Subject: Thank you for your inquiry\n\n";

#Print body of message
print MAIL <<"PrintTag";

Thank you for your interest in EMPE Media.  This e-mail confirms that your inquiry has been submitted.  We will review the information you have provided and will be in contact with you shortly.  We look forward to working with you.

Sincerely,

Meghan Paladino
EMPE Media
T.704.996.0845
F.803.980.3667
PrintTag

#Close sendmail program, releasing message to be sent
close (MAIL);

#Open sendmail program to send message to EMPE Media
open (MAIL, "|$mailprog -t") || die "Can't open mail program\n";

#Print message headers (To, Reply-To, From, Subject)
print MAIL "To: $mpmail\n";
print MAIL "Reply-To: $in{'email'}\n";
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Website:  Request for Quote\n\n";

#Print body of message
print MAIL <<"PrintTag";
Contact Information:
Name: $in{'name'}
Business: $in{'business'}
Address: $in{'address'}
City, State, Zip: $in{'city'}
Phone: $in{'phone'}
E-Mail: $in{'email'}
Referral Source: $in{'source'}
Keywords Used: $in{'searchwords'}

Web Service Interests are:
New Web: $in{'new_web'}
Redesign: $in{'redesign'}
Maintenance: $in{'maint'}
Web Marketing: $in{'marketing'}
Domain Registration: $in{'domainreg'}
Other: $in{'otherweb'}
Description of Other:
$in{'descotherweb'}

Current Web:
$in{'currentweb'}
Target Audience: $in{'targetaud'}
Number of Pages: $in{'pages'}
Anticipated Deadline: $in{'webdeadline'}
Additional Requirements: $in{'addreq'}
Reference Sites: $in{'samplesites'}

Graphic Design Interests are:
Logo: $in{'logo'}
Business Cards: $in{'buscards'}
Letterhead & Envelopes: $in{'lh&env'}
Brochure: $in{'broch'}
Postcard: $in{'postcard'}
Print Ad: $in{'ad'}
Other: $in{'othergraphic'}
Description of Other: $in{'descothergraph'}

Anticipated Deadline: $in{'graphicdeadline'}
Sample Logos: $in{'samplelogos'}

Other Comments: $in{'comments'}
PrintTag

#Close sendmail program, releasing message to be sent
close (MAIL);

#End of script
 
since there appears to be nothing wrong with the code, I have to assume it's the server. Could be overloaded or sendmail was having problems.


Side note: cgi-lib.pl should no longer be used (it's ancient, written for perl 4) port your code over to CGI.pm and use the built in security features at the minimum.
 
Kevin: Thank you for your response... I may call on you later for additional information on your side note (after I've made a decent attempt to learn more about your references on my own).

Now, the plot thickens:

I've determined that the script is now working as it ought to be when only the "required" fields are completed (name, telephone, and email address). Throw anything else in the mix and nothing happens.

So, any ideas what I'm missing? Thanks again!
 
Meghan,

Is this intentional?

You have this at the end of your validation script:
Code:
    else {
      alert("Thank you. \r \r An EMPE Media representative will contact you shortly.")
      window.close();
    }

return true
}

Maybe you have it there for test purposes, but if not you should remove the "window.close();".

Thanks,
-Mark
 
Mark: The window.close() function was intentional. The form pops in a new window, and it seemed most logical to close it upon completion. Is there a better way to close the window from within the CGI script (You'll have to forgive my piecemeal knowledge; so far, I only know what little I've used.)?

Thanks for the help!
Meghan
 
Mark: Fabulous! I removed the window.close(), and all is right with the world again. Now, back to my last post: Is there a way to close the window from within my CGI script? I could do a redirect, but it seems so much cleaner to just close it.

Thanks again!!!
Meghan
 
I use an almost identical version that i wrote for myself - however it must be quite a common way to solve the task as there can't be many ways to go about it:-

Code:
[b]#!/usr/bin/perl[/b]

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

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

open (MAIL, '| /usr/sbin/sendmail -t');

select MAIL;

print <<HERE;
To: recipient\@isp.co.uk
From: sender\@isp.co.uk
Subject: Form Parser Example
Content-Type: text/html
HERE

@pairs = split (/&/, $buffer);

print "<table border=1 cellpadding=3 cellspacing=0>\n";

foreach $pair (@pairs) {

  ($key, $value) = split (/=/, $pair);
  
     $key =~ tr/+/ /;
   $value =~ tr/+/ /;
     $key =~ s/%(..)/pack("c", hex($1))/eg;
   $value =~ s/%(..)/pack("c", hex($1))/eg;
   
  print "  <tr>\n";
  print "    <td>$key</td>\n";
  print "    <td>$value</td>\n";
  print "  </tr>\n";

}

print "</table>";

close MAIL;

select STDOUT;

print "Mail has been sent";

it doesn't require any modules (i know this is not necessarily a good thing - but it never lets me down!)


Kind Regards
Duncan
 
... note the location of my sendmail is /usr/sbin/sendmail - not the same location as yours


Kind Regards
Duncan
 
Duncan: Thank you. I ended up taking out the alert and close in the validation script I was using that Mark mentioned above and adding a redirect to the end of my CGI script that includes the content from the alert as well as a button to close the screen. Not quite as streamlined as I was hoping for, but it works.

Code:
{
#Print confirmation page
print "Location:[URL unfurl="true"]http://www.empemedia.com/confirm.html\n\n";[/URL]
}

I don't know what "select STDOUT;" means in your script, and now that I've got things working, I'm reluctant to try. Can you explain for me?

And really, if anyone can tell me how to close a window at the end of a CGI script, I'll pledge you my firstborn (she's getting to that surly pre-pubescent age...). Or, maybe I ought to start a new question...

Thanks again, all!
Meghan
 
Hi Meghan

No prob. Earlier in the script i use select MAIL - which is just a bit of shorthand so that any print statement is going to send it straight to the MAIL filehandle - then i simply change it back to the bog standard STDOUT with select STDOUT - which is where output normally goes. If i can help any further please shout


Kind Regards
Duncan
 
Meghan,

I believe you are describing what is or will at some point become a teenager. I'd rather work for a "thanks" if you don't mind. I have 2 already.

To have any sort of success with this you'll need to close the new window from the "opener".

tclose.html
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title></title>
<script type="text/javascript">
var nwwin;
  
function runClose(){
nwwin.close();
}

function nWindow(url){
nwwin = window.open('cgi-bin/tclose.pl','');
}
</script>
</head>
<body>
<a href="javascript:nWindow('cgi-bin/tclose.pl')">
New Window
</a>

</body>
</html>

tclose.pl
Code:
use strict;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header;
print start_html("Close This Window");

my $js =<<EOT;

<script type="text/javascript">
opener.runClose();
</script>

EOT
print $js;

print <<HTML;
<p>
If you see this the browser will not allow the script to close the window.
</p>
<a href="javascript:self.close();">Close this window</a>

HTML

print end_html;

Thanks,
Mark
 
Here is the full flight, with some checks that make it a little more predicatable.

tclose.html
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title></title>
<script type="text/javascript">
var nwwin;
  
function runClose(){
nwwin.close();
nwwin = null;
} 

function nWindow(url){
if(!nwwin)
nwwin = window.open(url,'');
}

</script>
</head>
<body>
<a href="javascript:nWindow('tcloseForm.html')">
New Window
</a>

</body>
</html>

tcloseForm.html
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title></title>
</head>
<body>
<form name="f1" action="cgi-bin/tclose.pl" method="post">
<input type="text" name="t1" size="25">
<input type="submit">
</form>

</body>
</html>

tclose.pl
Code:
use strict;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

my $query = new CGI;
print $query->header;

print $query->start_html("Close This Window");

my $js =<<EOT;

<script type="text/javascript">
if(opener && opener.runClose)
setTimeout('opener.runClose();', 2000);
</script>

EOT
print $js;

print <<HTML;
<p>
Thanks, Your Form Info Was SuccessFully Submitted..
</p>

HTML

print $query->end_html;

But what you might consider instead of having the CGI do the closing is...

Go ahead and send the user to the confirmation page and call the close function that is contained in the opener.

You could use "setTimeout()"(Javascript) to leave the window up for a couple of seconds.

You'll want to visit the Javascript Forum for help.

Thanks,
Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top