Solved

Sample code for form and parsing routine

Posted on 2002-04-22
9
364 Views
Last Modified: 2013-12-25
Hello

The points are for the code for a sample HTML form that uses POST to send it's data to the server, but mostly for the server side code (non java, prefer perl) that parses that data and returns an HTML page which displays the name=value pairs.
I'm trying for something that I can just drop in to an existing server (Apache 1.3.24 on FreeBSD) w/o hassling with environment set-up issues, so I figured perl/cgi wouldn't require any twiddling - just tell me where things need to go, and if it works, the points are yours.

Thanks,

v
0
Comment
Question by:vlg
  • 5
  • 4
9 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6960678
vlg,

"..prefer perl).."

Do you have the CGI module on your server?

if so then i can provide a very short & sweet code for you.

Let me know.
0
 

Author Comment

by:vlg
ID: 6960858
cgi is working, as it seems to by default - do you mean some separate cgi module that must be manually configured?

v
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6960895
vlg,

Here is a very basic Perl script that does not need any modules & should work for you, with bare minimum changes.

Posted below is a sample HTML file and the corresponding Perl script

Let me know how it goes.

==========contact.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><!-- #BeginTemplate "/Templates/template.dwt" -->
<!-- #BeginEditable "doctitle" -->
<TITLE>Consolidated Resorts Contact Form</TITLE>

<A HREF="http://www.experts-exchange.com/cgi/Q.20292319.html">http://www.experts-exchange.com/cgi/Q.20292319.html</A>
<P>

</HEAD>
<BODY leftMargin=0 topMargin=0 marginheight="0" marginwidth="0"  bgcolor="#FFFFFF" onLoad="MM_preloadImages('/sos2.jpg')">
<script type='text/javascript'>

function Go(){return}

</script>
<table width="754" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2"><img src="/header.jpg" width="775" height="110"></td>
  </tr>
  <tr>
    <td width="130" height="65" valign="top" bgcolor="#000000">

<A HREF="/index2.htm" TARGET="_self" onClick="window.open('http://www.consolidatedresorts.com/satellite.htm', 'satellite', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=250,height=250')" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image3','','/sos2.jpg',1)">
<img name="Image3" border="0" src="/sos1.jpg" width="130" height="22"></a>
      <br>
      <br>
      <br>
      <br>
      <br>
      <img src="/sunset-left.jpg" width="130" height="541"></td>
    <td width="648" height="65" valign="top">
      <table width="641" border="0" cellspacing="4" cellpadding="4">
        <tr>
          <td> <!-- #BeginEditable "content" -->  

              <a name="top"></a>

                            <div align="center"><font size="5" color="#00CC00" face="Verdana, Arial, Helvetica, sans-serif"><b><font color="#0000FF">Contact
                              Consolidated Resorts</font></b></font></div>
                           
                         
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>
                          <center>
                            <font face="Times New Roman, Times, serif" size="2">Please
                            complete all fields, and press the submit button below.<br>
                            Fields marked with an <font color="#0033FF">*</font>
                            are required. </font><font face="Arial, Helvetica, sans-serif" size="3"><br>
                            <font size="2" face="Times New Roman, Times, serif">Please
                            complete as much as possible so <b>Consolidated Resorts</b>
                            can best fulfill your vacation needs.</font></font>
                            <font size="2" face="Times New Roman, Times, serif"></font>
                          </center>
                        </td>
                      </tr>
                      <tr>
                       
                <td valign="top">
                <FORM METHOD="POST" ACTION="/cgi-bin/my_mailgate.pl">
<input type=hidden name="recipient" value="dhughes@crilv.com,mpalmer@lionzhead.com">
<input type=hidden name="subject" value="Contact Form From Consolidated Resorts Website">
<input type=hidden name="redirect" value="http://www.consolidatedresorts.com/thankyou.htm">
<table width="100%" border="0" cellpadding="0" height="596">
                            <tr height="20">
                              <td colspan="4" height="40"><font face="Times New Roman, Times, serif" size="4" color="#000000"><b></b></font>
                                <table width="100%" border="0" bgcolor="">
                                  <tr height="47">
                                    <td colspan="7" height="31" valign="top" align="left">
                                      <p><font size="4" color="#000000" face="Times New Roman, Times, serif"><b><font size="5">Contact
                                        Information</font></b></font></p>
                                    </td>
                                  </tr>
                                  <tr height="47">
                                    <td colspan="2" height="10" valign="middle">
                                      <font face="Times New Roman, Times, serif"><b><font size="3">
                                      <font color="#0033FF">Name:</font></font></b></font></td>
                                    <td colspan="5" height="10" valign="middle" width="490">
                                      <font face="Times New Roman, Times, serif">
                                      <input type="text" name="name" size="20">
                                      <font color="#0033FF" size="4">*</font></font>
                                    </td>
                                  </tr>
                                  <tr height="19">
                                    <td height="19" valign="middle" colspan="2">
                                      <div align="left"><font size="3" face="Times New Roman, Times, serif"><b><font color="#0033FF">E-mail:</font></b></font>
                                      </div>
                                    </td>
                                    <td colspan="7" height="28" width="490"> <font face="Times New Roman, Times, serif">
                                      <input type="text" name="email" size="20" value="" maxlength="50">
                                      <font color="#0033FF">* </font></font></td>
                                  </tr>
                                </table>
                                <table width="100%" border="0">
                                  <tr>
                                    <td valign="middle" width="9%"> <font size="3" face="Times New Roman, Times, serif"><b><font color="#0033FF">Address:</font></b></font>
                                    </td>
                                    <td valign="middle" width="55%"> <font face="Times New Roman, Times, serif">
                                      <input type="text" name="address" size="30">
                                      <font color="#0033FF" size="4">*</font></font></td>
                                    <td width="6%" valign="middle">
                                      <div align="right"><font size="3" face="Times New Roman, Times, serif"><b><font color="#0033FF">City:</font></b></font>
                                      </div>
                                    </td>
                                    <td colspan="3" valign="middle" width="30%">
                                      <font face="Times New Roman, Times, serif">
                                      <input type="text" name="city" size="10" maxlength="50">
                                      <font color="#0033FF" size="4">*</font></font>
                                    </td>
                                  </tr>
                                </table>
                                <table width="100%" border="0">
                                  <tr>
                                    <td width="9%" valign="middle"> <font size="3" face="Times New Roman, Times, serif"><b><font color="#0033FF">State:</font></b></font><font size="3" color="black" face="Times New Roman, Times, serif"><b>
                                      </b></font> </td>
                                    <td width="33%" valign="middle"><font size="3" color="black" face="Times New Roman, Times, serif"><b>
                                      <input type="text" name="state" size="5" maxlength="10">
                                      <font color="#0033FF" size="4">*</font>
                                      </b></font></td>
                                    <td colspan="2" valign="middle"> <font face="Times New Roman, Times, serif"><b><font size="3" color="#0033FF">ZipCode:</font></b>
                                      <input type="text" name="zip" size="10" maxlength="10">
                                      <font size="4" color="#0033FF">*</font></font>
                                    </td>
                                    <td colspan="2" valign="middle" width="30%">
                                      <font size="3" face="Times New Roman, Times, serif"><b><font color="#0033FF">Country:</font>
                                      </b></font> <font face="Times New Roman, Times, serif">
                                      <input type="text" name="country" size="10" maxlength="15">
                                      <font size="4" color="#0033FF">*</font></font>
                                    </td>
                                  </tr>
                                </table>
                                <table width="100%" border="0">
                                  <tr>
                                    <td width="31%" valign="middle"> <font face="Times New Roman, Times, serif"><b><font size="3" color="#0033FF">Home
                                      Phone Number:</font></b></font></td>
                                    <td width="69%" valign="middle"> <font face="Times New Roman, Times, serif">
                                      <input type="text" name="homephone" size="15" maxlength="20">
                                      <font size="4" color="#0033FF">*</font></font>
                                    </td>
                                  </tr>
                                </table>
                                <font face="Times New Roman, Times, serif"></font></td>
                            </tr>
                            <tr height="20">
                              <td colspan="3" height="40"><font face="Times New Roman, Times, serif" size="4" color="#000000"><b><font size="5">Vacation
                                Profile</font></b></font></td>
                              <td height="40" valign="top" width="101">&nbsp;</td>
                            </tr>
                            <tr height="20">
                              <td colspan="4" height="40">
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td width="62%" height="123" valign="middle"><b><font face="Times New Roman, Times, serif" size="3" color="#0033FF">Please
                                      Select The Consolidated Resort <br>
                                      You Are Inquiring About</font><font face="Times New Roman, Times, serif" size="3"><br>
                                      </font></b><font face="Times New Roman, Times, serif" size="3">
                                      <font size="2">(To Select Multiple Hold
                                      Down the &quot;Ctrl&quot; Button)</font></font></td>
                                    <td width="38%" height="123" valign="middle">
                                      <select name="select" multiple size="6">
                                        <option value="Club de Soleil, Las Vegas">Club
                                        de Soleil, Las Vegas</option>
                                        <option value="Sands of Kahana, Hawaii">Sands
                                        of Kahana, Hawaii</option>
                                        <option value="Kahana Villa Resort, Hawaii">Kahana
                                        Villa Resort, Hawaii</option>
                                        <option value="Maui Beach Club, Hawaii">Maui
                                        Beach Club, Hawaii</option>
                                        <option value="Hono Koa Resort, Hawaii">Hono
                                        Koa Resort, Hawaii</option>
                                        <option value="The Gardens, Hawaii">The
                                        Gardens, Hawaii</option>
                                        <option value="Maui Banyan, Hawaii">Maui
                                        Banyan, Hawaii</option>
                                        <option value="Kona Islander Inn, Hawaii">Kona
                                        Islander Inn, Hawaii</option>
                                        <option value="Kahana Beach, Hawaii">Kahana


===================my_mailgate.pl
#!/usr/local/bin/perl

$|++;

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

print "Here<P>\n";

&GetInput;

foreach (sort keys %form){
  print $_," = ",$form{$_},"<BR>\n";
}

sub GetInput {
  $buffer = "";
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  @pairs=split(/&/,$buffer);
  foreach $pair (@pairs){
    ($key,$value)=split(/=/,$pair);
    $value =~ s/\+/ /g;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/~!/ ~!/g;
    $value =~ s/[\n\r]/ /sg;  #remove \n
    $value =~ s/\[\]//g;  #remove []
    $form{$key}=$value;
  }
}

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Expert Comment

by:maneshr
ID: 6961104
vlg,

"... do you mean some separate cgi module that must be manually configured?..."

Yes. i meant a seperate module called CGI.pm

Here is a small test script which will tell you if you have that module installed on your server or not.

NOTE: since the perl interpreter is located in /usr/local/bin directory on my server i have used it.
You might have to change it based on the location of that interpreter on your server.

Let me know how this script works for you.

#!/usr/local/bin/perl

$|++;

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

use CGI;

$q=new CGI;

print "CGI exists!!<BR>\n";
0
 

Author Comment

by:vlg
ID: 6962898
Hello maneshr

>Here is a small test script which will tell you if you have that module installed on your server or
not.

I ran it, and I got:
(offline mode: enter name=value pairs on standard input)

What does that signify?

Thanks

v


0
 

Author Comment

by:vlg
ID: 6962988
Hello again maneshr

I fixed up the html you provided, and put in up on the server.  In the same directory as the index page, I created a subdirectory called cgi-bin, and that's where I put the my_mailgate.pl.
I chmod+x my_mailgate.pl also.
When I enter data in your html form, I get a 404 page that says The requested URL /cgi-bin/my_mailgate.pl was not found on this server.
I'm sure I'm missing something simple, any ideas?

v
0
 
LVL 16

Accepted Solution

by:
maneshr earned 175 total points
ID: 6963327
vlg,

"..and put in up on the server...."

Can you post the URL of that page here?

"..                     I created a subdirectory called cgi-bin, .."

Unfortunately, having CGI capabilities might not be as simple as creating a cgi-bin directory.

Where is your site hosted? is it on your own server or at a hosting location?

Is there a server admin you can contact to find out if you have CGI access of the server?

The question at this point goes much more basic than your original question. Therefore, i want to be sure you have CGI access before proceeding any further.

Let me know.
0
 

Author Comment

by:vlg
ID: 6963637
I forgot to configure Apache  - all is now well

Thanks

v
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6963731
vlg,

"..I forgot to configure Apache  - all is now well.."

Excellent!!! Great news!!!

Glad to know you got the solution you were looking for.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now