Web Analytics Made Easy -
StatCounter CGI Contact Form - CodingForum

Announcement

Collapse
No announcement yet.

CGI Contact Form

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CGI Contact Form

    Hi, just bought some hosting etc for my dad thinking it was all good from 123-Reg... But it didnt come with PHP so i need to do a contact form in CGI and i need some help :P

    For the feild boxes i have the following code:

    Code:
              <td align="left" valign="top"><form method="POST" action="/cgi-bin/contact.pl">
    <span class="style3">Fields marked (*) are required</span>
    <p><span class="style3">Email From:* </span><br>
    <input name="EmailFrom" type="text" class="style3">
    <p><span class="style3">Subject: </span><br>
    <input name="Subject" type="text" class="style3">
    <p><span class="style3">Name:</span><br>
    <input name="Name" type="text" class="style3">
    <p><span class="style3">Address:</span><br>
    <input name="Address" type="text" class="style3">
    <p><span class="style3">Telephone:</span><br>
    <input name="Telephone" type="text" class="style3">
    <p><span class="style3">Query:</span><br>
      <textarea name="Query" cols="50" rows="10" class="style3"></textarea>
    <p><input type="submit" name="submit" value="Submit">
    </form>
    And that works all fine. I created the CGI-Bin folder and used this code below, to save it i put it in notepad and saved it as "contact.pl" but i have no idea if this is correct

    Code:
    #!/usr/bin/perl
    use CGI::Carp qw(fatalsToBrowser);
    use CGI qw(:standard);
    print "Content-type: text/html \n\n";
    
    # get posted data into local variables
    $input = new CGI;
    $EmailFrom = $input->param('EmailFrom'); 
    $EmailTo = "jon.*****@*****.com";
    $Subject = $input->param('Subject'); 
    $Name = $input->param('Name'); 
    $Address = $input->param('Address'); 
    $Telephone = $input->param('Telephone'); 
    $Query = $input->param('Query'); 
    
    # validation
    $validationOK=true;
    if ($EmailFrom eq '') {$validationOK=false;}
    if ($validationOK eq false) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }
    
    # prepare email body text
    $Body .= "Name: ";
    $Body .= "$Name";
    $Body .= "\n";
    $Body .= "Address: ";
    $Body .= "$Address";
    $Body .= "\n";
    $Body .= "Telephone: ";
    $Body .= "$Telephone";
    $Body .= "\n";
    $Body .= "Query: ";
    $Body .= "$Query";
    $Body .= "\n";
    
    # send email 
    $mailprog = '/usr/sbin/sendmail -t';
    open(MAIL,"|$mailprog");
    print MAIL "To: $EmailTo\n";
    print MAIL "From: $EmailFrom\n";
    print MAIL "Subject: $Subject\n\n";
    print MAIL $Body;
    close(MAIL);
    
    # redirect to success page 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
    This dosnt seem to work :| i get internal server error. Please help.

    Thanks Con

  • #2
    don't use unqouted strings whenever possible:

    Code:
    # validation
    $validationOK=true;
    if ($EmailFrom eq '') {$validationOK=false;}
    if ($validationOK eq false) {
    change to:

    Code:
    # validation
    $validationOK='true';
    if ($EmailFrom eq '') {$validationOK='false';}
    if ($validationOK eq 'false') {
    and this variable should be single quoted:

    Code:
    $EmailTo = 'jon.*****@*****.com';

    the @ symbol is used to define an array. Inside of double-quoted strings perl will try and convert arrays into strings. Just use single quotes to avoid that problem or escape the @ with a \ inside of double-quoted strings to avoid interpolation:

    Code:
    $EmailTo = "jon.*****\@*****.com";
    the rest of the code looks like it should work. Make sure to upload the script in ASCII mode and set the permissions properly, probably 755. You can use your FTP client to set the permissions (chmod).

    Comment


    • #3
      How do i do that? I am completely new to all this :P

      The site is www.bathtimberframe.com
      Last edited by Conneh; Oct 15, 2006, 06:53 AM.

      Comment


      • #4
        How did you upload the script to your website? If you used an FTP program read the help files for "transfer mode" and "chmod".

        Comment


        • #5
          I used SmartFTP, its chmod to 755, but to save the file i done it in notad and saved it as contact.pl

          Comment


          • #6
            Using notepad is fine.
            When you upload to the web server ensure you have the transfer mode set to "ACSII" or "TEXT". You will need to do this only for pl or cgi files. HTML and GIF's etc will work on "BINARY" mode.
            I don't use SmartFTP so I don't know how to set those options.

            Comment


            • #7
              Thanks, i done all this but still get internal server error :[

              Comment


              • #8
                Originally posted by Conneh View Post
                I used SmartFTP, its chmod to 755, but to save the file i done it in notad and saved it as contact.pl
                Yes, that is fine. Perl scripts are just text files and have to be saved as plain text files like notepad does by default. If you use a word processor just make sure to save perl files in ASCII or plain text mode instead of a word processing mode like .doc which adds extra stuff to the file.

                Notepad is OK but you might find it's limitations irritating at times. If you want to write perl scripts use an editor like http://www.indigostar.com/perledit.html

                Comment


                • #9
                  Try this:

                  Code:
                  #!/usr/bin/perl
                  print "Content-type: text/html \n\n";
                  print "Hello world";

                  if the above does not work then the very first line might be wrong or the permissions are not getting set correctly or you are not transfering the file in the correct mode to the server.

                  Comment


                  • #10
                    Hmm, tried it all but still getting internal server error. Thanks for the help.

                    Comment


                    • #11
                      Ok, just done a few things to the html code, and now i get "Hello World" when i click submit, this has gave me some hope lol

                      this is the code for it to send to the email

                      Code:
                      $EmailTo = "jon.wilcox1\@virgin.net";
                      But im not receiving any emails :P
                      Last edited by Conneh; Oct 15, 2006, 04:56 PM.

                      Comment


                      • #12
                        chnage this:

                        Code:
                        # send email 
                        $mailprog = '/usr/sbin/sendmail -t';
                        open(MAIL,"|$mailprog");
                        print MAIL "To: $EmailTo\n";
                        print MAIL "From: $EmailFrom\n";
                        print MAIL "Subject: $Subject\n\n";
                        print MAIL $Body;
                        close(MAIL);
                        to:

                        Code:
                        # send email 
                        $mailprog = '/usr/sbin/sendmail -t';
                        [B]open(MAIL,"|$mailprog") or die "Can't open email: $!";[/B]
                        print MAIL "To: $EmailTo\n";
                        print MAIL "From: $EmailFrom\n";
                        print MAIL "Subject: $Subject\n\n";
                        print MAIL $Body;
                        close(MAIL);
                        see if the die function returns an error message

                        Comment


                        • #13
                          I just get "hello world"

                          Comment


                          • #14
                            post the latest version of the script you are using.

                            Comment


                            • #15
                              Here

                              Code:
                              #!/usr/bin/perl
                              print "Content-type: text/html \n\n";
                              print "Hello world";
                              
                              # get posted data into local variables
                              $input = new CGI;
                              $EmailFrom = $input->param('EmailFrom'); 
                              $EmailTo = "jon.wilcox1\@virgin.net";
                              $Subject = $input->param('Subject'); 
                              $Name = $input->param('Name'); 
                              $Address = $input->param('Address'); 
                              $Telephone = $input->param('Telephone'); 
                              $Query = $input->param('Query'); 
                              
                              # validation
                              $validationOK='true';
                              if ($EmailFrom eq '') {$validationOK='false';}
                              if ($validationOK eq 'false') {
                                print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
                                exit;
                              }
                              
                              # prepare email body text
                              $Body .= "Name: ";
                              $Body .= "$Name";
                              $Body .= "\n";
                              $Body .= "Address: ";
                              $Body .= "$Address";
                              $Body .= "\n";
                              $Body .= "Telephone: ";
                              $Body .= "$Telephone";
                              $Body .= "\n";
                              $Body .= "Query: ";
                              $Body .= "$Query";
                              $Body .= "\n";
                              
                              # send email 
                              $mailprog = '/usr/sbin/sendmail -t';
                              open(MAIL,"|$mailprog") or die "Can't open email: $!";
                              print MAIL "To: $EmailTo\n";
                              print MAIL "From: $EmailFrom\n";
                              print MAIL "Subject: $Subject\n\n";
                              print MAIL $Body;
                              close(MAIL);
                              
                              # redirect to success page 
                              print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";

                              Comment

                              Working...
                              X