KingPin's Forum
 
K.P.s.N. Register vbBux / KPs Mall Bugs Blogs FAQ Search Today's Posts Mark Forums Read Donate
Go Back   KingPin's Forum > KP's Network Forum > Tutorials & Stuff!
Reload this Page PHP Tutorial #8 : Feedback Form Script
 


Tutorials & Stuff! This is a discussion on PHP Tutorial #8 : Feedback Form Script in the Tutorials & Stuff!;
Description: Feedback Form Script Before you write your first PHP program, make sure that that your website is running on a ...

Reply
 
LinkBack Thread Tools
PHP Tutorial #8 : Feedback Form Script
(#1 (permalink))
Old
KingPin's Avatar
KingPin is Offline
Da Boss!
KingPin has disabled reputation
 

My Mood:
 
Posts: 7,034
Thanks: 20
Thanked 17 Times in 15 Posts
Blog Entries: 22
Join Date: Apr 2004
Location: Brooklyn, NY
PHP Tutorial #8 : Feedback Form Script - July 10th, 2004

Feedback Form Script

Before you write your first PHP program, make sure that that your website is running on a web host that runs PHP 4.1 or above.
2.

You may also find it useful to have a copy of PHP 4.1 or later installed on your own computer. This makes testing your PHP scripts much easier.
3.

And of course, you need an ASCII text editor of some kind (such as Notepad on Windows).
4.

This tutorial also assumes that you have at least some knowledge of HTML. This is necessary because if I have to explain all the HTML tags as well, this tutorial will wind up being tediously long.

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 develop that script (and others) to include features commonly found in such FormMail scripts.

If you are programming-savvy, you will recognize this as a sort of "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 section of an HTML file named, say, feedback.html.

Code:
<form method="post" action="sendmail.php">

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

 Message:<br />

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

 </textarea><br />

 <input type="submit" />

</form>
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
tag) to a script named "sendmail.php" (also specified in the tag).
The Feedback Form PHP Script

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

Code:
<?

 $email = $_REQUEST['email'];

 $message = $_REQUEST['message'];



 mail( "yourname@KingPinServers.com", "Feedback Form Results",

  $message, "From: $email" );

 header( "Location: http://www.KingPinServers.com/thankyou.html" );

?>
When the form is submitted to sendmail.php, the contents of the "email" field in the form is put into a PHP variable called $_REQUEST['email']. Likewise the contents of the "message" field is put into the variable $_REQUEST['message'].

If you had named the fields in your form "emailofsender" and "contentsofmessage", then the information submitted in those fields would have been available to your script in the variables $_REQUEST['emailofsender'] and $_REQUEST['contentsofmessage'] respectively. I think you get the idea.

The first thing we do in our PHP script is to make the information that is submitted easily accessible to the rest of the program.

Firstly, we made a copy of the contents of $_REQUEST['email'] in a variable we call $email. This was done in the line
$email = $_REQUEST['email'] ;

Note that we don't really have to call this new variable $email - we could have called it $thingamajig if we wished, but it makes sense to name a variable with some meaningful name.

Likewise, in the next line, we made a copy (assigned) of $_REQUEST['message'] in a variable $message.
$message = $_REQUEST['message'] ;

Again, we could have named the new variable anything we wanted - but it's easier for us to understand the program if the variable name reflects what it does.

The real workhorse of this script is in the line beginning with "mail".

mail( "yourname@KingPinServers.com", "Feedback Form Results",
$message, "From: $email" );

mail is a special function in PHP that sends mail. The first parameter to mail is supposed to contain the email address you want the form contents to be sent to, such as 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 the 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 also 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 will be "From: william@shakespeare.com".

You can also use single quotes (such as those in '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".

You can't interpolate variables like $_REQUEST['email'] and $_REQUEST['message'] in the same way, which is why we assigned their contents to $email and $message. The latter are also easier to read anyway.

Finally, it is appropriate to thank the visitor for his message. This is done with the line
header( "Location: http://www.KingPinServers.com/thankyou.html" );

This line causes PHP to send an HTTP header back to the visitor's browser telling it to load the URL "http://www.KingPinServers.com/thankyou.html". 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.

 
Reply With Quote
Revenue Shared Ads
PHP Tutorial #8 : Feedback Form Script
Revenue Shared Ads
Reply

Bookmarks

Tags
php, tutorial, feedback, form, script

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Rupees Per Thread View: 1.00
Rupees Per Thread: 15.00
Rupees Per Post: 5.00
Forum Jump



These are the 100 most searched terms
Search Cloud
_backup.rc antrix wow antrix wow server autocad 2007 keygen best google gadgets best wow character bt map devil may cry 4 crack enchanting leveling erika bella fuckteam fuckteam 5 georgina lempin gigistar gigistar topless kpsden kpsforum leatherworking guide leveling enchanting mangos custom vendors mangos item id mangos linux mangos server mangos spell id mangos wotlk mature creampie milena velba no rest for the ass object id wow phoenix marie this is sparta vipersdenforums voyage century bot windows activation workaround windows xp lite wotlk alpha private server wotlk private server wotlk server wotlk wiki wow leatherworking guide wow mount locations wow object id wow object id list wow object id's wow object ids wow objects id www.adobeflashplayer www.adobeflashplayer.com www.kpsforum.com xbox 360 mmorpg ... powered by Simple Search Cloud
Powered by vBulletin® Version 3.8.0 Beta 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright 2004-2009 KPsN


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82