Sunday, December 19, 2010

Writing Your First PHP Script: Feedback Form

I have always believed that the most fun way to learn a new programming language (whether it is a language like C or a scripting language like PHP), is to use it to write a real-life useful program. Of course this is not the most systematic method of learning, but it works well if you already have some background in programming.

The first thing to do before writing a PHP program is to have two things:

* a web host offering PHP where you can use the program you write
* PHP installed on your own computer.

If you use Windows, you can find some tips on the above in my article on "How to Install PHP3 on Windows" at:
http://www.thesitewizard.com/archive/installphp.shtml

I will begin with a very rudimentary (but working) PHP script to take input from a feedback form and send it to you in an email message. This type of form is sometimes referred to as a FormMail or Form to Mail script. In later articles, I will probably develop that script (and others) to include features commonly found in such FormMail scripts.

I will have to assume that you have some knowledge of HTML code, otherwise this tutorial will wind up being tediously long. If you need help with HTML coding, you might try the beginner's guide at:
http://www.ncsa.uiuc.edu/General/Internet/WWW/HTMLPrimer.html

For those who are programming-savvy, this is sort of like a "Hello World" program, but infinitely more useful!
WRITING THE FEEDBACK FORM

The first thing we need to do is to write the feedback form itself. Put the following code in the BODY section of a HTML file named, say, feedback.html.
Email:

Message:



Basically the form asks the visitor for his email address (the field named "email" found in "INPUT name=email" above) and message (the field named "message" found in "TEXTAREA name=message"), and presents him with a button which he can click to submit the contents of the form. When the form is submitted, it is "posted" (see the "method" attribute of the FORM tag) to a script named "sendmail.php3" (also specified in the FORM tag).
THE FEEDBACK FORM PHP SCRIPT

Now all that remains is to code "sendmail.php3". This is made extremely easy by the facilities available in PHP. Type the following code into a file named "sendmail.php3". Do not put anything else into that file, ie, don't put in any other HTML tags or headers, etc.

When the form is submitted to sendmail.php3, the contents of the "email" field in the form is put into a PHP variable called $email. Likewise the contents of the "message" field is put into the variable $message.

All the script has to do is to call a special function appropriately called "mail" which would do the actual sending of the email. The first parameter to mail is actually the email address you want the form contents to be sent to, ie, your own email address. The second parameter is the "Subject" of the email message. The last two parameters are the content of the message and any other headers you want sent, respectively. 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.

Notice that, like many other programming languages, strings (sequences of characters) are enclosed in double quotes, such as "Feedback Form Results".

Variables like $message can be used as-is. Note also that you can actually interpolate (introduce) the contents of the variable $email into a string, like "From: $email", so that if your $email string contained an address like william@shakespeare.com, the final string that is passed to the mail function is actually "From: william@shakespeare.com".

You can also use single quotes (like 'Hi there') to quote strings, but when you do so, the variables included are not expanded. This is useful if, for some reason, you really want to pass the string 'From: $email' to mail without PHP translating that to "From: william@shakespeare.com".

Finally, it is appropriate to thank the visitor for his message. To do this, we send a HTTP header back to his browser telling his browser to load a file called "thankyou.html" from your site. The "header" function allows us to send any HTTP header to the browser.

You will of course have to create such a file called "thankyou.html" with some sort of message to thank your visitor for his efforts, otherwise your visitor will be greeted with an unfriendly "404/File Not Found" error after he sends his message. You should also replace the URLs and email addresses with the correct ones if you want to use that script on your site.

By the way, the script has to be enclosed within the "" tags because the PHP processor treats all input as HTML code unless otherwise specified. On some systems, you may need to use "" as the opening and closing tags to get the script to work.

Easy wasn't it? In just a few lines, you've written your first PHP script. And it's not some trivial and useless script - it is actually a working, usable program!

In later tutorials, I will develop that script so that your visitor's input is checked (eg, to catch instances where someone accidentally clicks the "Submit" button before they fill in their email address), and even integrate both the form and the script into a single "feedback.php3" file, just like the one you can find at thesitewizard.com.

No comments:

Post a Comment

Confused? Feel free to ask

Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.

Note:
Please do not spam Spam comments will be deleted immediately upon my review.