PDA

View Full Version : need help in making simple cgi form.


cryonic
04-08-2006, 04:33 PM
Hi, i am not so familar with cgi so i need help here. The form is available here but it isn't working yet. I haven't got any idea how and what to put in the <form> 'action' tag yet. we dont want to put an 'action="mailto:info@sos.com".'

Can anyone generous enough to help us prepare a small simple working cgi script (formtoemail.cgi)? There is a cgi-bin folder at where the html form is located but its empty at the moment. The server is a unix based server. I can setup a email account on this server. The server is capable of handling cgi scripts.

http://www.oopsmural.com/cocci/testform.htm


html form html code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MYTC INQUIRY</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form method="post" action="???.cgi" name="inquiry">
<table width="95%" border="1" align="center" cellpadding="4" cellspacing="1" bordercolor="#000000" bgcolor="#CCCCCC">
<tr bgcolor="#0066CC">
<td colspan="2"><div align="center"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF">Montfort
Youth Training Center (Sabah) ONLINE EMAIL FORM INQUIRY:</font></b></div></td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"> <font size="2" face="Arial, Helvetica, sans-serif">*Name
/ Organisation:</font></td>
<td> <input name="name" type="text" id="name" size="60"></td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"> <font size="2" face="Arial, Helvetica, sans-serif">I/C
No (New):</font></td>
<td> <input name="ic" type="text" id="ic" size="30"></td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"> <font size="2" face="Arial, Helvetica, sans-serif">*Tel
no:</font></td>
<td> <input name="tel" type="text" id="tel" size="20"></td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">Office
no: </font></td>
<td><input name="office" type="text" id="office" size="20"> </td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">Hand
phone no: </font></td>
<td><input name="hp" type="text" id="hp" size="20"> </td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">Occupation</font></td>
<td><input name="job" type="text" id="job" size="50"> </td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">*Address:</font></td>
<td class="font1"><textarea name="address" cols="50" rows="5" id="address"></textarea></td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">Email:</font></td>
<td class="font1"><input name="email" type="text" id="email" size="30"></td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">Homepage:
</font><font size="2" face="Arial, Helvetica, sans-serif">&nbsp; </font></td>
<td><input name="web" type="text" id="web" size="50"> </td>
</tr>
<tr bgcolor="#E8E8E8">
<td width="25%" valign="top" class="font1"><font size="2" face="Arial, Helvetica, sans-serif">*Your
Message: </font><br> <br> </td>
<td bgcolor="#E8E8E8" valign="top"><textarea name="message" cols="41" rows="11"></textarea>
</td>
</tr>
<tr bgcolor="#E8E8E8">
<td><div align="left" class="font2"></div>
<div align="center"><em>*must be filled</em></div></td>
<td><div align="center">
<input type="submit" name="submit" value="Submit" class="input" >
<input name="reset" type="reset" class="input" value="reset">
</div></td>
</tr>
</table>
</form>
</body>
</html>

mlseim
04-08-2006, 05:51 PM
I wish there was a sub-section on the Perl Forum just for form processing.

This is such a popular issue.

Before you upload a form script, I'm going to save a lot of headaches
by having you upload a simple script, just to see if your cgi-bin is
working, you know how to upload it and apply script permissions.

Copy and paste this script into Notepad, name it "hello.cgi" .... then
upload the script in ASCII mode and CHMOD to 755 (using your FTP program)
into your cgi-bin directory.
#!/usr/bin/perl -w

use CGI ':standard';
use strict;

my $test = param('test');

print "Content-type: text/html\n\n";
print "Hello World! <br><br>";
print "<br><br>\n";

Now, execute the script and see if it says "Hello World!" on your browser.
Run it by typing: http://www.oopsmural.com/cgi-bin/hello.cgi

If you get this far, let us know.

If you can't get it work, let us know ... but also search on Google for
some beginner Perl tutorials. You have to get this working before you'll
ever get a form processor script working.

Also ... on your form, you have required fields.
Technically, I would avoid Javascripting and test those fields
with the Perl script, but since you're looking for something easy
and simple, you should search Google for some Javascript methods
to validate your form before it gets sent to the Perl script.

cryonic
04-08-2006, 06:46 PM
yeah, me too.

Anyway, run the test. it worked. i am aware of the 'required fields.' We can leave it alone for now. It worked in the subdomain as well.

http://www.oopsmural.com/cgi-bin/hello.cgi
http://www.oopsmural.com/cocci/cgi-bin/hello.cgi

mlseim
04-09-2006, 01:17 AM
OK, now you're going to edit the script below
and upload it the same way. You'll put in all the
variables you used in your form and the email
address where the data will be sent, and the web
page where it will return when it's done.

Enter the subject with the $subject variable.
Do that in the script. Don't let anyone enter the subject
from the form ... same thing with $recipient (your email address).

#!/usr/bin/perl

use CGI ':standard';

########################################################

$mailprogram = "/usr/sbin/sendmail"; #<-- Location of your sendmail program
$myemail = "email\@comcast.com"; #<-- Your email address

########################################################
# Read Variables from the form ...
# You'll enter each one of your unique variables in this section.
# The word in the parethesis is the name of your variable in your form.
# Add as many as you need.

$sender=param('email');
$name=param('name');
$tel=param('tel');
$ic=param('ic');
$office=param('office');
$whatever=param('whatever');

$redirect="http://www.yoursite.com/thankyou.html";
$subject="Email from our form.";

########################################################

# the \n means 'new line' or linefeed ...

open (MAIL, "|$mailprogram -t") or die "Can't fork sendmail.\n";
print MAIL "To: $myemail\n";
print MAIL "From: $sender\n";
print MAIL "Subject: $subject\n\n";
print MAIL "=====================================================\n";
print MAIL "\n";
print MAIL "All your variables here.\n";
print MAIL "Just keep adding these lines as needed ... \n";
print MAIL "Name: $name \n";
print MAIL "Phone: $tel \n";
print MAIL "Office Phone: $office \n";
print MAIL "etc ... etc ... you get the idea ... \n";
print MAIL "\n";
print MAIL "=====================================================\n";
print MAIL "\n";
close(MAIL);

# goto the redirect page
print "Location: $redirect\n\n";

cryonic
04-10-2006, 05:29 AM
OK basically i made the changes i needed (still learning PERL like what you suggested me to do). Maybe you can help me check a little bit. Now my question is about the 'sendmail', i have problem locating where is it. Am i supposed to be looking for a 'sendmail.cgi' file? I cant seem to locate it in the directory yet.

in the html form code<form method="post" action="???.cgi" name="inquiry">
'action' will be the location of where this cgi codes is located.
$mailprogram is the location of the sendmail which is used to send the form
$myemail is the intended target where the form will be sent.
Please correct me if i am wrong.


#!/usr/bin/perl

use CGI ':standard';

########################################################

$mailprogram = "/usr/sbin/sendmail"; #<-- Location of your sendmail program
$myemail = "test@oopsmural.com"; #<-- Your email address

########################################################
# Read Variables from the form ...
# You'll enter each one of your unique variables in this section.
# The word in the parenthesis is the name of your variable in your form.
# Add as many as you need.

$sender=param('email');
$name=param('name');
$ic=param('ic');
$tel=param('tel');
$office=param('office');
$hp=param('hp');
$job=param('job');
$address=param('address');
$web=param('web');
$message=param('message');

$redirect="http://www.oopsmural.com/cocci/thankyou.html";
$subject="Inquiry.";

########################################################

# the \n means 'new line' or linefeed ...

open (MAIL, "|$mailprogram -t") or die "Can't fork sendmail.\n";
print MAIL "To: $myemail\n";
print MAIL "From: $sender\n";
print MAIL "Subject: $subject\n\n";
print MAIL "=====================================================\n";
print MAIL "\n";
print MAIL "\n";
print MAIL "\n";
print MAIL "Name: $name \n";
print MAIL "Ic: $ic \n";
print MAIL "Phone: $tel \n";
print MAIL "Office Phone: $office \n";
print MAIL "Handphone no: $hp \n";
print MAIL "Occupation: $job \n";
print MAIL "Address: $address \n";
print MAIL "Homepage: $web \n";
print MAIL "Message: $message \n";
print MAIL "\n";
print MAIL "\n";
print MAIL "=====================================================\n";
print MAIL "\n";
close(MAIL);

# goto the redirect page
print "Location: $redirect\n\n";

mlseim
04-10-2006, 02:09 PM
You are correct on all three ...

'action' will be the location of where this cgi codes is located.
$mailprogram is the location of the sendmail which is used to send the form
$myemail is the intended target where the form will be sent.
Please correct me if i am wrong.

$mailprogram is the location of the sendmail.
That path shown is most common, so try it as is ...
If you get a failure and it's caused by that line, you'll have to
ask your webhost for the correct path to "sendmail". You won't
see the file in your cgi-bin ... it's a server thing.

========================

On this line:
$myemail = "test@oopsmural.com"; #<-- Your email address

You might want to put in an "escape" character if your script
works, but you don't get any emails - in front of the @ ...

$myemail = "test\@oopsmural.com"; #<-- Your email address

========================

And your form "action" points to your script (whatever you called it).

Other than that, everything looks fine. If you ever make any additions
or changes and the script fails, always look for obvious things like,
misplaced quotes, missing ; at the end of a line, or mis-typing a
bracket / parenthesis ( { } ) they sometimes look the same after you
stare at it for a while. Another trick ... you can always leave a line in
place, but "comment" it out (making it ignore the line), by putting a
# in front of the line. If your script fails and you are suspicious about
a section of your script, comment out a bunch of lines until you figure
out where the offensive error is located (usually a typo).