How to Create a Feedback Form for Your Site

User avatar
Site Admin
Posts: 35
Joined: Sun Jul 03, 2016 2:17 pm
Location: India

How to Create a Feedback Form for Your Site

Postby admin » Sun Jul 03, 2016 5:10 pm

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

Code: Select all

<title>Feedback Form</title>
<h1> Fill in the form below to send us your comments <h1>
<form method="post" action="feedback.php">
Name: <input name=”name” type=”text” />

Email: <input name="email" type="text" />


<textarea name="comments" rows="15" cols="40">

<input type="submit" />

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’:

Code: Select all

$msg = "Name: $name \n";
$msg .= "Email: $email \n";
$msg .= "Comments: $comments \n";
$recipient = "[email protected]";
$subject = "New Feedback from my site";
$mailheaders = "From: $email \n";
$mailheaders .= "Reply-To: $email \n";
mail($recipient, $subject, $msg, $mailheaders);

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

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

mail(“[email protected]”, “email subject”, “the message”,
“From: [email protected]”);

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
Finest web hosting solution at the most affordable rates
Cheap Website Hosting
cheap VPS hosting india

Return to “Scripts / Code Snippets”

Who is online

Users browsing this forum: No registered users and 0 guests