Creating a Feedback Form (PHP)
This tutorial will teach you how to create a feedback form in php. This can be used as an order form or a contact me form for your website.
To start with we will create the form. This is what the user will see on your web page. The form below has three text areas: name, email address and feedback. It also sends the IP address of the user.
Code:
<?php
$ipaddress = getenv("REMOTE_ADDR");
?>
<html>
<head>
<title>Feedback Form </title>
</head>
<body>
<form action="send.php" method="POST">
<input type=”hidden” name=”ipaddress” value=”$ipaddress”>
<p>Your Name:<br> <input type="text" size="12" name="name" id="name"></p>
<p>Your Email Address:<br> <INPUT type="text" size="12" name="email" id="email"></p>
<p>Feedback:<br>
<textarea name="feedback" cols="25" rows="5"></textarea></p>
<p><input type="submit" value="Send Feedback"></p>
</form>
</body>
</html> Make sure you name this file feedback.php when you save it. So what happens in this file? I won’t go into much of the HTML- hopefully you know that already. The key lines are:
<?php
$ipaddress = getenv("REMOTE_ADDR");
?>
Here, a section of PHP is denoted by its opening and closing tags,
<?php and
?>. In the php snippet, we use getenv to retrieve the “environmental variable” called remote_addr- the remote IP address accessing the script. A final line to note in this script is:
<form action="send.php" method="POST">
This tells our browser to use the POST method to send data to our file “send.php”.
Next we will create the script which actually sends the feedback form.
Name the following script send.php and save it.
Code:
<html>
<head>
<title>Feedback Form </title>
</head>
<body>
<?php
echo "<p>Thank-you, <b>$_POST[name]</b>, for your message</p>";
echo "<p>Your email address is: <b>$_POST[email]</b></p>";
echo "<p>Your message was:<br>";
echo "$_POST[feedback] </p>";
//Start building the email
$msg = "Name: $_POST[name]\n";
$msg = "IP Address: $_POST[ipaddress]\n";
$msg .= "E-Mail: $_POST[email]\n";
$msg .= "Feedback: $_POST[feedback]\n";
//Set up the email headers, subject and recipient
$recipient = "ADD YOUR EMAIL ADDRESS HERE";
$subject = "Feedback from www.yourdomain.com";
$mailheaders = "From: My Web Site <address@yoursite.com> \n";
$mailheaders .= "Reply-To: $_POST[email]";
//Send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
</body>
</html>
Again in this script we have used
<?php and
?> to enclose our php code. It’s important to remember those in all of your php scripts, even if they are pure PHP. Next, we use the
echo command to output HTML to the browser. Echo works just like
write in Javascript. Remember not to use any “ characters within the echo command, or you will get a nasty error. Moving on a few lines, we come to
$msg = "Name: $_POST[name]\n";
This is an important part of our script, as it sets up the “body” of our email. We start building the string $msg with the name of our user. To retrieve his/her name, we call the name variable from the array created by the POST method in feedback.php. To end the line, we use a \n. This tells the script to add a newline character- making our email look nicer. Our next line,
$msg .= "E-Mail: $_POST[email]\n";
Introduces another operator, the “concatenate equals”. If you don’t speak geek, that basically means
add something to a string that you’ve already started, in this case
$msg. Skipping a few lines, we come to setting the
mail headers. Mail headers are used to help address the email. Take special care to format these lines correctly, as they are often used to help anti-spam programs. Finally, we send the email using the
mail function.
mail($recipient, $subject, $msg, $mailheaders);
The parameters required to send an email, recipient, subject, message body and mailheaders are added, contained within brackets, and then the function closed.
Do NOT remove the following when redistributing this article.
Copyright CodingTalk.com - All Rights Reserved
http://www.codingtalk.com