How Do I Create a Fill-in Email Form?
One of the most common ways to receive feedback from your website visitors is to provide an email form. It can provide a means of receiving suggestions, questions or comments. Knowing that they can get in touch with you can also increase your users' confidence in your website. Create a contact or email form for your users to fill in using HTML and PHP.
Instructions
-
-
1
Open the file to which you will add the email form and insert the following:
<form action="contact.php" method="post" name="contact" ><table>
<tr>
<td valign="top">Email:</td>
<td><input name="email" type="text" /></td></tr>
<tr>
<td valign="top">Name:</td>
<td><input name="name" type="text" /></td></tr>
<tr><td valign="top">Subject:</td><td><input name="subject" type="text" /></td></tr>
<tr>
<td valign="top">Message:</td>
<td><textarea name="message" cols="50" rows="10">
-
2
Open a new file. Save it as "contact.php" and insert the following:
<?php
// The data to submit
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = myemail@mail.com' . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email.. "\r\n";
$body = "Name: ".$name. "<br/>";
$body .= "Email: ".$email. "<br/>";
$body .= "Subject: ".$subject. "<br/>";
$body .= "Message: ".$message. "<br/>";
// Send
mail($to, $subject, $body, $headers);
header( 'Location: '.$_SERVER['HTTP_REFERER']);
?>
Replace "myemail@mail.com" with your email address. The message entered on your form is emailed to you. Users are returned to the page they were on by the "$_SERVER['HTTP_REFERER']" value.
-
-
3
Save the file. Upload all files and test the form by entering the page in your Web browser.
-
1
References
- Photo Credit pc connecting email handdrawn image by patrimonio designs from Fotolia.com