Sunday, June 24, 2007

Using PHP to Process a Simple Feedback Form

In this article I explain how to create a simple feedback form, and how to send the data entered on that form to an email address.

Two web pages need to be produced to perform this task:

feedback.htm - the feedback form itself, and processData.php - the form handler that processes the data entered on the form

feedback.htm
1. < html >
2. < head >< /head >
3. < body >
4. < table cellpadding="6" >
5. < tr >
6. < td bgcolor="#E0F0F0" style="border-top:1px solid #009999;border-right:1px solid #009999;border-left:1px solid #009999;border-bottom:1px solid #009999;" >
7. < h2 >Feedback Form< /h2 >
8. If you want to provide any feedback on this website, either positive or negative, please use this form to do so.


9. < form name="form1" method="post" action="processData.php" onSubmit='return checkfields()' >
10. Subject
11. < br >< input type="text" name="title" size="50" >


12. Enter text
13. < br >< textarea rows="8" cols="43" name="comments" >< /textarea >


14. From (your email address)
15. < br >< input type="text" name="from" size="50" >


16. < br >
17. < input type="submit" name="Submit" value="Send Feedback" >


18. < /form >
19. < /td >
20. < /tr >
21. < /table >
22. < /body >
23. < /html >

This is the form presented to the web site visitor. There are only three fields, but you could obviously have as many as you wanted to on your own form.

When the web site visitor clicks the Send Feedback button, the data entered into the three fields is sent to processData.php.

processData.php
1. < ?php
2. // This page sends the feedback entered in the form to an email address
3. $title=$_REQUEST['title'];
4. $comments=$_REQUEST['comments'];
5. $from=$_REQUEST['from'];
6. // Email feedback to a specified email address
7. $email = "someone@somewhere.com";
8. // The subject
9. $subject = "Feedback from website";
10. // The message
11. $message = "Title:".
12. "$title".
13. "Comments:".
14. "$comments".
15. "From:".
16. "$from";
17. mail($email, $subject, $message, "From: $from");
18. ? >

19. < html >
20. < head >
21. < /head >
22. < body >
23. Thank you for your feedback.


24. < br >
25. Kind Regards


26. < /body >
27. < /html >

(Please note: lines 11 to 16 should really include a few backslash n's to introduce a few line breaks into the email message. Unfortunately, due to display considerations, I had to drop them from this sample code.)

There are two main areas to this web page: a PHP script at the top and an HTML area at the bottom. The PHP script gets the data from the form and puts them into three variables ($title, $comments, and $from). A subject variable ($subject) is also defined. The PHP mail function processes these variables and generates an email message that is sent to the email address defined in $email.

The bit of HTML at the bottom of the web page simply displays a message to the web site visitor after the email has been sent.
About the Author: John Dixon is a web developer and technical author. These days, John spends most of his time developing dynamic database-driven websites using PHP and MySQL.

Go to http://www.computernostalgia.net to view one of John's sites. This site contains articles and photos relating to the history of the computer.

To find out more about John's company, John Dixon Technology Limited, go to http://www.dixondevelopment.co.uk.

Article Source: http://EzineArticles.com/?expert=John_Dixon

No comments: