Me Pez Me Pez



Home| Links Info| Partners| About Us| Listings
Register | Login  | Tutorials| Policies| Contact Us

 !    Tutorial Three - PHP Component of a Simple Form page 2

Tutorial 3 - Part II -Flying into Formation - page 2

O. K., so your 'submit-if' statement worked. Now the rest is easy. You want an email sent to you with your guest's input. Use this code filling in the underlined with your own personal info:

$date = date('F j, Y G:i:s');
$first = stripslashes ($_POST['firstname']);
$last = stripslashes ($_POST['lastname']);
$email = stripslashes ($_POST['email']);
$comment = stripslashes ($_POST['comments']);
$refer = getenv("HTTP_REFERER") ;

$mailto = "you@youremail.com";
$mailfrom .= 'From: YOUR_WEBSITE.COM' . "\r\n";
$subject = "Guest Comment";
$body = "$date From: $refer.\n A guest, $first $last\n whose return email is: $email\n has made this comment:\n $comment\n";
mail($mailto,$subject,$body,$mailfrom);


First, copy the above code. (Highlight the box and hit "edit & copy" on your browser. Then paste to notepad or another text editor.) In notepad or another text editor, change you@youremail.com to your email address and fill in the "mailfrom" variable with the name of your website. Save this altered code so you can copy it to the appropriate place in your "comment.php." Go to the top of your page of code. In your opening php code, after the opening "<?php" and the opening echo statement (echo ('<p>Form was submitted.</p>');) but before the opening "else" bracket ({), insert the above altered code. Your "comment.php" should now resemble this example: click here.

Save your comment.php and load it up to your web site. Look at the comment.php in your web browser. If everything looks good, fill out the form and submit it. To see how your form should act click here. You should receive an email (at the email address you provided) with all the information you submitted in the guest comment form. If you received the email, you're done, right? Well, not exactly. (Maybe, in an ideal world, you're done, but not in my world. In my world, the party of god does the work of the devil - ruining the lives of the innocent. This is the world I live in. I can hardly consider it the 'best of all possible worlds.' I can't possibly see it as an ideal world. But if you think your world is an ideal world and, better still, if you're sure of it, then you are done. You have a form whose input will be emailed to you.)

For me and others like me, however, there's still a number of things that have to be done. But first let's review the above code. The above code starts with a list of variables - everything that begins with the dollar sign ($). The first variable - $date - is unnecessary because your email comes dated. However I include this variable ($date) in case you also want to save this information to a file somewhere. The snippet of code that your date variable is equal to "date('F j, Y G:i:s')" is just one way of defining how you want a date to appear. The term "stripslashes' is used to rid your email and file of slashes that show up before quotes. "Magic_quotes_gpc" is on by default and is the reason for these slashes. The next four variables are input from your form. You create these variables so you can move your guest comment input to your email. The 'refer' variable is included so you can check to make sure the input came from your site. The next four variables ($mailto, $mailfrom, $subject, and $body) are used to simplify the email. They are not the only variables you could use. You could use '$email_to' in place of '$mailto'. You could add headers. You could use '$message' in place of '$body.' You could even use the one variable '$message' to transfer all of your form variables to your email, like so: alt_send_email











The periods ( . ) in the above lines are used to concatenate your sub-strings into sentences. The final line, "mail( $mailto, $subject, $body, $mailfrom or $headers)", in both email examples, gives the command to do the email. If you're fussy about the email you get and don't like seeing the word "node", you might want to add headers to the original boxed code above. That would change your code to:

$date = date('F j, Y G:i:s');
$first = stripslashes ($_POST['firstname']);
$last = stripslashes ($_POST['lastname']);
$email = stripslashes ($_POST['email']);
$comment = stripslashes ($_POST['comments']);
$refer = getenv("HTTP_REFERER") ;

$mailto = "you@youremail.com";
$subject = "Guest Comment from YOUR_WEBSITE.COM";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
$headers .= 'From: you <you@youremail.com>' . "\r\n";
$body = "$date From: $refer.\n A guest, $first $last\n whose return email is: $email\n has made this comment:\n $comment\n";
mail($mailto,$subject,$body,$headers);


(Note: In filling in the 'From' header above, use a second email address if you have one.)

Now, if you also want to save this information to a file on your site, do the following:

  • Open notepad and save the blank notepad as guests.txt.
  • Upload guests.txt to your site.
We'll call our info 'guest_data' and save it to 'guests.txt' in a tabbed array (notice the "\t" between varaibles below). Add the following code after your mail command, "mail( $mailto, $subject, $body, $mailfrom or $headers)", but before the opening 'else' bracket ( { ).:

$guest_data = "$date\t$refer\t$first\t$last\n\t$email\n\t$comment\n";
if ($fp = @fopen ("guests.txt", "a")) { // Open the file for writing.
fwrite ($fp, $guest_data);
fclose ($fp);
echo ('<p>Program executed O.K.</p>');
echo ('<p>Info filed away in a tabbed array for future reference.</p>');
}

Don't forget the final bracket ( } ). With these changes. your "comment.php" should now look like this : click here. (If all else fails, you can always copy this code by hitting "edit & copy" in your browser.) To test this code, do the following:    First when you get tired of receiving email, just add two slashes ('//') one space in front of your 'mail' command line. That will stop the email. Save your 'comment.php' again, reload to your site's public_html, open 'comment.php' in your web browser, fill out the form and submit it. To check to see if the code worked, open 'guests.txt.' in your browser. You may have to hit your browser's "refresh" button, but the info you submitted should be easily readable. If it is and you're happy with it, then go back to your 'file write' command and place two slashes ('//') a space in front of your 'file write' command to stop the command until you're ready to use it. stop filing


Now you're ready to adapt your form for an imperfect world.


To finish your form, continue on page 3



Flying Into Formation - Part II - pages . . . 1 . . . 2 . . . 3 . . . 4 . . . 5 . . . 6 . . . 7 . . .

Your IP address is: 38.107.191.99
 !   Sponsors

We'd like to thank our sponsors for their encouragement and support. If you can use their services, please support us by supporting them.


 !   Dev Shed
Dev Shed - The Open Source Web Development Site Dev Shed
Powered By Dev Shed

More >>
 !   Dev Shed Forum
Dev Shed - The Open Source Web Development Site Dev Shed
Powered By Dev Shed

More >>
 !   Tutorials





Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

adsense_anim
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
More >>

©2005 ME PEZ BLOGSNOVELSESCAPESHOMEFAQsPOLICIES
REGISTERLOGINFORGOT YOUR PASSWORD?