Join us on Facebook

Please wait..10 Seconds Cancel

9.15.2013

// // Leave a Comment

Creating a simple feedback form using PHP


Creating a simple feedback form using PHP is very easy, thanks to PHP's prebuilt mail() function. In this tutorial I'll show you 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!";
?>
That is it!

0 comments:

Post a Comment