PHP Scripting/Programming Tutorial for Beginners 2 PHP Tutorials
PHP Scripting Tutorial for Beginners 2



May 1st, 2009:

PHP Scripting Tutorial 2

How to create a simple mail form
(also knows as contact form or feedback form)

In PHP tutorial II I will show you how to create a simple contact form or mail form (feedback form). If you are not familiar with HTML please visit

This is a Contact, Feedback, or Mail Form:

You can just copy and paste the form into your HTML page wherever you want your contact form to appear: (Let's call this HTML page contact.html)
<form method="post" action="contact.html">
  Your Email: <input name="mail" type="text" /><br />
  Message:<br />
  <textarea name="message" rows="20" cols="30">
  </textarea><br />
  <input type="submit" />
</form>
This is your mail or contact form where people can enter their email address and their message.

Now you need to add the block of PHP commands, which will tell the PHP engine on your server what to do with the information entered into your mail or contact form. This block can be inserted into the same HTML page that contains your contact form (in this case paste the PHP code into the very top of your HTML page, above the <HTML> and the <HEAD> tag and specify the path to the PHP script in your <form method="post" action="contact.html"> as the same HTML file that contains your mail form), or it can be added in a separate file like this: Open your ASCII text editor (for example Notepad). Copy and paste the PHP code below into a blank document and save it as contact.php and put it into the same directory where your HTML file with the contact from is located.

<?
  $mail = $_REQUEST['mail'] ;
  $message = $_REQUEST['message'] ;
  mail( "youremail@youraddress.com", "Message From Contact Form",
  $message, "From: $mail" );
  header( "Location: http://www.yourdomain.com/thankyou.html" );
?>

Once the "Submit" button of your mail form in your HTML page is being clicked, the <form method="post" action="contact.html"> (or contact.php) executes the PHP script in your .html (or .php) file, which tells the PHP engine to send the message to the email address you have specified in this line of your PHP script,

mail( "youremail@youraddress.com", "Message From Contact Form",
and then forwards the visitor to the "Thank You" page specified in
header( "Location: http://www.yourdomain.com/thankyou.html" );
You do not have to forward the mail sender to a "Thank You" page, but it looks more professional and it's just polite to thank the sender for contacting you. The "Thank You" page should also contain information about how long it will take for you to respond.

To sum this up: Your PHP contact or mail script includes 2 (3) files:


contact.html - The HTML file containing your mail or contact form
thankyou.html - The "Thank You" page where you direct the mail sender to after clicking "Submit"
(contact.php - The PHP file containing the contact form PHP script - this script can be added to the contact.html as well)

Now, this is a pretty mundane and simple PHP contact form which works fine for small personal websites, but if you want to give people who contact you more options and collect more information like their first name, last name or company name along with their message, you might want to check out the PHP contact, mail, or feedback form at:

© This Article is Copyrighted by WebmasterSeminar.com

Please do not copy or reproduce this article in whole or part, in any form, without mentioning the source and including a live link back to WebmasterSeminar.com - Thank You!

LINK TO THIS PHP TUTORIAL:


Copy the code below and paste into your web page
<a href="http://www.webmasterseminar.com/phptutorial2.php">
Webmaster Seminar</a><br />
PHP Tutorial for Beginners II

PHP Related Links