Simplest Feedback form in PHP
It is very easy to develop feedback from almost in all languages but PHP makes it very clear and simple for you, thanks to PHP’s prebuilt mail() function. In this tutorial you will see a complete, working example.
Firstly, lets create the HTML portion of the form:
<form action="sendmail.php" method="POST"> <b>Your name :</b> <input type="text" name="name"><br> <b>Your e-mail :</b> <input type="text" name="email"><br> <b>Message</b><br><textarea name="message"></textarea> <input type="submit" value="Send"><input type="reset" value="Clear"> </form>
This is a basic form with two input fields and a textarea. Since we are using the POST method, we’ll be eliciting PHP’s $_POST variables within our PHP script to retrieve what’s been entered. Inside the PHP script called sendmail.php” (as that’s what we’re specifying above in our form), enter the following:
/*Here we are going to declare the variables*/ $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; //Save visitor name and entered message into one variable: $formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message"; $recipient = "you@yourdomain.com"; $subject = "Contact Form";
Obviously, you’ll need to change the recipient to your own email address. If you want to send it to more than one email address, just put a comma in between them. You can also change the subject. By using the $_POST array of PHP, which is an associative array, we can access all the elements of the form.
Looking back at the first input field in the form, you will see name=”name”.
This is how we got the variables $_POST['name'] and $_POST['email'] and $_POST['message'] above- they came from each form element’s given name. In other words, you use each form element’s name attribute as the key to accessing its value within array $_POST.
Continuing with our “sendmail.php” script, we now need to construct a mail header which mail() requires before it can send the form to
you:
$mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "MIME-Version: 1.0\r\n";
These are simple mail headers. The first two tell the email program who it is from and who to reply to. The last one tells the MIME-Version. All this information will be included in the email that’s sent to you. Finally, we’re now ready to invoke the mail() function to actually send the form:
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure");
echo "Thank You!";
This executes the mail function with four parameters. It mails to the recipient with a subject, message, and headers. If it does not execute (die), it will print “Failure!”.
And now the complete “sendmail.php” script:
<?php
/*Here we are going to declare the variables*/
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "you@yourdomain.com";
$subject = "Contact Form";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
echo "Thank You!";
?>
It should be working!
Waqar Hussain
a project management professional and blogger who write about project management and entrepreneurship in Pakistan
Website - Twitter - Facebook - More Posts
Join the mailing lists
Recent Stories
- Motivate your employees for more sucess
- Why we Pakistanis reach late at work?
- Why Pakistani entrepreneurs wait till disaster for fixing their business?
- Learn to say “NO” to your bosses
- Time management is the key to efficiency
- De-Variables a new entrepreneur from Pakistan
- Importance of Team in Project
- Workshop on “Arena Multimedia Specialist Pro”
- Why Ecommerce is essential for business in Pakistan?
- Why good feasibility report is required?
Facebook Fan Page
Twitter Feeds
- So far great parties for #EBS #GoLive 12 hrs ago
- Congratulation #TeamPakistan 1 day ago
- Innovation Pakistan looking for bloggers! Message me 1 day ago
- Hello all! Morning! Hows everyone at twitter? 1 day ago
- Any one have an idea about ptcl EVO 3.1 mbps service in North Karachi..... 4 days ago
- Yes! We are live with Oracle EBS R12 Supply Chain and Financial Modules! We seem junior & inexperience, but yet we have done GURU's work! 4 days ago
- I am not tweeting and it does not mean that I do not exists! :P 1 week ago
- Hello!!!!! No twitter for this month! no no no I am not protesting for #Aafia :p Stuck at office for work! :S 3 weeks ago
- RIP Arfa karim! We proud on you! 3 weeks ago
- RIP! Arfa karim! A little legend of Pakistan! We proud on u! 3 weeks ago
- More updates...
Posting tweet...












