Sei sulla pagina 1di 6

How to Create a PHP Contact Form  2008 

How to Create a PHP


Contact Form

Written by:
David Friedman

March 27, 2008


1
How to Create a PHP Contact Form  2008 

Table of Contents 
Introduction ................................................................................................ 3 
Materials Needed ........................................................................................ 3 
Instructions ................................................................................................. 3 
Source Code ................................................................................................ 4 
References .................................................................................................. 6 

2
How to Create a PHP Contact Form  2008 

Introduction
This is a supplemental manual that will assist you when viewing my tutorial on how to
create a PHP contact form. The intended audience for this tutorial is for someone who
has basic HTML and PHP knowledge. My tutorial will assist you in developing a simple
PHP contact form for your web site so that a user can send an email directly to you.

Materials Needed
ƒ Desktop computer or laptop
ƒ Internet Access
ƒ Text Editor
ƒ Web Browser
ƒ A Web Site

Instructions

1. Open text editor


a. Create a new HTML file

2. Write code for HTML contact form


a. Start with blank HTML page
b. Add labels for text boxes
c. Create text boxes
d. Create submit button

3. Open browser
a. Test to make sure the form looks correct

4. Write code for the PHP script


a. Define variables for email, subject, and message
b. Write mail function
c. Print confirmation message to the screen

3
How to Create a PHP Contact Form  2008 
5. Upload both files to your web site
a. Index.php
b. Mailform.php

6. Open browser
a. Test the mail form to make sure the email was sent

7. Check your email to make sure it worked

Source Code

1. Index.php:

“<form method='post' action='Mailform.php'>

<p>Email: <input type='text' name='email'></p>

<p>Subject: <input type='text' name='subject'></p>

<p>Message:<br/>

<textarea name='message' rows='10' cols='30'></textarea></p>

<input type='submit' value='Submit Form'>

</form>”

2. Mailform.php:

“<?php

$email_address = $_POST['email'] ;

$subject = $_POST['subject'] ;

$message = $_POST['message'] ;

mail( "someone@example.com", "Subject: $subject",

$message, "From: $email" );

4
How to Create a PHP Contact Form  2008 
echo "Thank you for using our mail form.<br/>";

echo "Your email has been sent.";

?>”

Source code provided by W3Schools.com

5
How to Create a PHP Contact Form  2008 

References
ƒ W3CSchools : World Wide Web Consortium. Retrieved March 24, 2008, Web
site: http://www.w3schools.com/php/php_mail.asp (for providing the source code)

Potrebbero piacerti anche