Form help! Completely lost!

Associate
Joined
4 Oct 2004
Posts
67
Hi there, I really need some help.

I'm trying to create a webpage, where users input there details and then they get emailed to me.

But the problem im having is getting started! I've made the form on my webpage, but i have no idea how to set up the server side, so it sends me the email.

Im trying to create something like this: https://www.overclockers.co.uk/webnote.php

I've tried to search for an answer but its all to complicated.

I've tried to do it using perl and cgi - just cant get it to work.

So if anyone can help me that would be great!

Btw my host is www.register1.net
 
http://www.pixel2life.com/tutorials/php_coding/forms/

many good tutorials you will find there.

here is a simple example code that you could simply just copy and paste and name the page its on to contact.php or if tis differnt look at the code until you see contact.php and change it to whatever the name of the file is, and that it, done.

Code:
<?php
if ($_POST['formsent'] == "1")
{
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $youremail = $_POST['replyto'];
  $message = $_POST['message'];
  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  {
	echo "The e-mail was not valid. Please go back and try again.";
  }
  else
  {
	if($subject == "")
	{
	  echo "You must enter a email subject";
	}
	else
	{
	  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $youremail))
	  {
		echo "Your e-mail was not valid. Please go back and try again.";
	  }
	  else
	  {
		if ($message == "")
		{
		  echo "You must enter a message";
		}
		else
		{
		  
		  $headers = "From: ".$youremail;
		  mail($email,$subject,$message,$headers);
		  echo "
Thank you for filling out the online form.";
		}
	  }
	}
  }
}
 
Thanks for the reply, but i must be doing something so wrong, as i still cant get it to work.

Again it seems to be the server side. :(
 
this is the php - for the form handler that i got from the net. I really have no idea what im doing so any help would be greatful

PHP:
#!/usr/bin/perl
#
#   mailer.pl-- A simple program to mail form data to an email address
#
#   Written in 1997 by James Marshall, [email protected]
#   For the latest, see http://www.jmarshall.com/easy/cgi/
#

# IMPORTANT: MAKE SURE THESE TWO VALUES ARE SET CORRECTLY FOR YOU!
$mailprog= "/usr/lib/sendmail" ;
$recipient= "[email protected]" ;      # make sure to \ escape the @

# Get the CGI input variables
%in= &getcgivars ;

# Open the mailing process
open(MAIL, "|$mailprog $recipient")
    || &HTMLdie("Couldn't send the mail (couldn't run $mailprog).") ;

# Print the header information
$ENV{'HTTP_REFERER'} || ($ENV{'HTTP_REFERER'}= "your Web site") ;
print MAIL "Subject: Form data from the Web\n\n",
           "The following data was entered at $ENV{'HTTP_REFERER'}:\n\n" ;


# Find length of longest field name, for formatting; include space for colon
$maxlength= 0 ;
foreach (keys %in) {
    $maxlength= length if length > $maxlength ;
}
$maxlength++ ;

# Print each CGI variable received by the script, one per line.
# This just prints the fields in alphabetical order.  To define your own
#   order, use something like
#     foreach ('firstname', 'lastname', 'phone', 'address1', ... ) {
foreach (sort keys %in) {

    # If a field has newlines, it's probably a block of text; indent it.
    if ($in{$_}=~ /\n/) {
        $in{$_}= "\n" . $in{$_} ;
        $in{$_}=~ s/\n/\n    /g ;
        $in{$_}.= "\n" ;
    }

    # comma-separate multiple selections
    $in{$_}=~ s/\0/, /g ;

    # Print fields, aligning columns neatly
    printf MAIL "%-${maxlength}s  %s\n", "$_:", $in{$_} ;
}


# Close the process and mail the data
close(MAIL) ;


# Print an HTML response to the user
print <<EOF ;
Content-type: text/html

<html>
<body>
<h3>Your data has been sent.</h3>
</body>
</html>
EOF

exit ;


#-------------- start of &getcgivars() module, copied in -------------

# Read all CGI vars into an associative array.
# If multiple input fields have the same name, they are concatenated into
#   one array element and delimited with the \0 character (which fails if
#   the input has any \0 characters, very unlikely but conceivably possible).
# Currently only supports Content-Type of application/x-www-form-urlencoded.
sub getcgivars {
    local($in, %in) ;
    local($name, $value) ;


    # First, read entire string of CGI vars into $in
    if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||
         ($ENV{'REQUEST_METHOD'} eq 'HEAD') ) {
        $in= $ENV{'QUERY_STRING'} ;

    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i) {
            length($ENV{'CONTENT_LENGTH'})
                || &HTMLdie("No Content-Length sent with the POST request.") ;
            read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;

        } else { 
            &HTMLdie("Unsupported Content-Type: $ENV{'CONTENT_TYPE'}") ;
        }

    } else {
        &HTMLdie("Script was called with unsupported REQUEST_METHOD.") ;
    }
    
    # Resolve and unencode name/value pairs into %in
    foreach (split(/[&;]/, $in)) {
        s/\+/ /g ;
        ($name, $value)= split('=', $_, 2) ;
        $name=~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge ;
        $value=~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge ;
        $in{$name}.= "\0" if defined($in{$name}) ;  # concatenate multiple vars
        $in{$name}.= $value ;
    }

    return %in ;

}


# Die, outputting HTML error page
# If no $title, use a default title
sub HTMLdie {
    local($msg,$title)= @_ ;
    $title= "CGI Error" if $title eq '' ;
    print <<EOF ;
Content-type: text/html

<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<h3>$msg</h3>
</body>
</html>
EOF

    exit ;
}

#-------------- end of &getcgivars() module --------------------------
 
I'm no expert (at all in fact - I'm just starting to look at PHP also), but there's a nice tutorial which I can point you to...

PHP Tutorial

Check out the section on form handling and sending emails. (7 & 10) :)
 
ok i've sorted a few more things out now, and stripped it down to the bare minimum to try and figure out why its still not working.

check out www.interlink4u.com/form.htm

Im now getting the internal error message, but i've googled so much my google button is broken.
 
Ermm - sorry but can you explain that any more? I am so new to this i dont know what to do really.

this is what the log is saying:

Code:
[Fri Feb 09 01:03:17 2007] [error] [client 80.229.243.205] File does not exist: /home/virtual/site106/fst/var/www/html/essential.gif, referer: http://www.interlink4u.com/form.htm
[Fri Feb 09 01:03:17 2007] [error] [client 80.229.243.205] Directory index forbidden by rule: /home/virtual/site106/fst/var/www/html/, referer: http://www.interlink4u.com/form.htm
[Fri Feb 09 01:03:18 2007] [error] [client 80.229.243.205] Premature end of script headers: test.cgi, referer: http://www.interlink4u.com/form.htm
[Fri Feb 09 01:03:18 2007] [error] [client 80.229.243.205] Directory index forbidden by rule: /home/virtual/site106/fst/var/www/html/, referer: http://www.interlink4u.com/form.htm
 
Last edited:
Edit the email address and save this to test.php and run it on your server to check mail works ok

Code:
<?php
$to = "You";
$to_email = "youremailaddress";
$subject = "An email from php";
$mailheaders = "From: My Website <$to_email>\n";
$mailheaders .= "Reply-To: $to_email";
$mailheaders .= "To: Me <$to_email>\r\n";
$mailheaders .= "MIME-Version: 1.0";

$body = "Test Text sent " . date("g:i:sa j/n/Y") . "\r\n";
$body .= "\r\n";
$body .= "----------\r\n";
$body .= "\r\n";
$body .= "End of message\r\n";
$body .= "\r\n";
$body .= "----------\r\n";
$body .= "\r\n";				
			
mail("$to_email", "$subject", "$body", "$mailheaders");
?>
 
paulsheffII - thank you so much for your help its been great. Without it, im sure i'd have been lost for many more days!

Now its time to get it to work with frontpage forms and i'll be set!
 
Back
Top Bottom