iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 20, 2008
Wiki: A Solution to the Web Design Problem
About
 
July 19, 2008
Plurk or Twitter: Which one is Better?
About
 
July 19, 2008
Top 10 Social Networking Sites
About
 
July 19, 2008
Create Menus with Lists and CSS
About
 
July 17, 2008
Poll: What do you do when you can't design?
About
 
July 17, 2008
22 Reasons to Plurk
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Email Systems

Building a Feedback Form in PHP 

  Views:    8446
  Votes:    4
by Ahmad Permessur 4/29/04 Rating: 

Synopsis:

In this tutorial we will learn how to email content collected through a form to a specified email address.
Pages: 
The Article

I’ll assume that you know some basics HTML forms syntax. If not, don’t worry, you’ll quickly pick it up as we move along. Before we get started, make sure that your webhost supports PHP.

Our feedback form

We will first create the HTML page that will contain our form. Copy and paste the code below in your favorite text editor (e.g. Notepad), and name the file as feedback.html

<html>
<head>
   <title>Feedback Form</title>
</head>
<body>

<h1> Fill in the form below to send us your comments <h1>

<form method="post" action="feedback.php">
  Name: <input name=”name” type=”text” /><br />
  Email: <input name="email" type="text" /><br />
  Comments:<br />
  <textarea name="comments" rows="15" cols="40">
  </textarea><br />
  <input type="submit" />
</form>

</body>
</html>

 
If you open this file (feedback.html) in your browser, you’ll see 3 fields; a name, an email and a comment text field. Your visitors can fill in the form and once they click the ‘submit’ button, that data is sent to feedback.php for processing.

You can also create your form in any WYSIWYG editor (like FrontPage, Dreamweaver), provided that you correctly name your form elements, and use the POST method along with the PHP file that will handle the content collected through the form. You can also add more fields accordingly.


The feedback form PHP script

Our next step will be to create the PHP file ‘feedback.php’, which will handle the data collected through the form, and email it back to you.

So here, we go. Once again, fire up your text editor, and copy and paste the code below, and save the file as ‘feedback.php’:

<?php

$msg = "Name:t$namen";
$msg .= "Email:t$emailn";
$msg .= "Comments:t$commentsn";

$recipient = "youremail@domain.com";
$subject = "New Feedback from my site";

$mailheaders = "From:$emailn";
$mailheaders .= "Reply-To:$emailnn";

mail($recipient, $subject, $msg, $mailheaders);
header("Location:
http://www.yoursite.com/thankyou.html");
?>

When the content is send to feedback.php, the content in the text field “name” will be put into the variable $name. Likewise the content of email and comments will be put into the variable $email and $comments respectively.

So if you’ve created additional fields, you can just place the dollar sign $ in front of the name, to declare a variable containing the content collected in that particular field.

You’ll also noticed that we’ve declared another variable $msg that will make a copy of the different variables and this will be the body of our email. The t before the variable will return a tab, while the n after the variable name will make our move the content that follows to the next line. They basically just provides some formatting to our message.

The $recipient variable will contain the email address where you want the data collected to be emailed, while the $subject variable will contain the email subject. $mailheaders is another variable that will contain the from email address and reply-to email address. We want a "From:" header so that we know who is sending the email to us and can reply to him/her if we need to. The From & Reply-to are set to the senders address.

Now here comes the magic! mail() is a special PHP function that allows you to send an email with only one line of coding. In general the mail function is as follows:

mail(“youremail@domain.com”, “email subject”, “the message”,
“From:
email@domain.com”); 

Finally, we will redirect our visitor to a ‘Thanks you’ html page, using the header() function in PHP. All you need to do is to create a thanks you page, and save it as HTML, and add the link to the header() function.

That brings us to the end of this tutorial. You can use JavaScript to validate your visitor’s input and you can also extend this tutorial to create survey forms, or even ‘recommend a friend’ form.

Pages: 

Similar/related articles:


 
  Sponsors