A simple feedback form usually contains fields for the respondent's name and e-mail address, and a text area for some sort of message. In this section, you create two files: one for the feedback form, and one for the PHP script to process the form, send the mail, and return a response to the browser.
In this section, you create the first half of the form/script combo—the feedback form itself, often referred to as the front-end form.
Open a new file in your text editor.
Type the following HTML:
<HTML> <HEAD> <TITLE>Simple Feedback Form</TITLE> </HEAD> <BODY>
Begin your form. Assume that the method is POST and the action is a script called send_simpleform.php:
<FORM METHOD="POST" ACTION="send_simpleform.php">
Create an input field for the user's name with a text label:
<P><strong>Your Name:</strong><br> <INPUT type="text" NAME="sender_name" SIZE=30></P>
Create an input field for the user's e-mail address with a text label:
<P><strong>Your E-Mail Address:</strong><br> <INPUT type="text" NAME="sender_email" SIZE=30></P>
Create a text area to hold the message with a text label:
Add a submit button:
<P><INPUT TYPE="submit" NAME="submit" VALUE="Send This Form"></P>
Close your form and add some more HTML so that the document is valid:
</FORM> </BODY> </HTML>
Save the file with the name simple_form.html, and place this file in the document root of your web server.
Open your web browser and type http://127.0.0.1/simple_form.html.
You should see a form containing a text field for the person's name, a text field for the person's e-mail address, a text area for the message, and a button that says Send This Form.
In the next section, you create the back-end script. That script will expect three variables: $_POST[sender_name], $_POST[sender_email], and $_POST[message].
According to the form action in simple_form.html, you need a script called send_simpleform.php. The goal of this script is to accept the text in $_POST[sender_name], $_POST[sender_email], and $_POST[message] format, send an e-mail, and display a confirmation to the web browser.
Open a new file in your text editor.
Begin a PHP block, and then add some error-checking code into the mix:
<?
if (($_POST[sender_name] == "") ||
($_POST[sender_email] == "") ||
($_POST[message] == "")) {
header("Location: simple_form.html");
exit;
}
Start building a message string, which will contain the content for the e-mail itself:
$msg = "E-MAIL SENT FROM WWW SITE\n";
Continue building the message string by adding an entry for the sender's name:
| Note |
The next few steps will continue building the message string by concatenating smaller strings to form one long message string. |
Continue building the message string by adding an entry for the sender's e-mail address:
$msg .= "Sender's E-Mail:\t$_POST[sender_email]\n";
Continue building the message string by adding an entry for the message:
Create a variable to hold the recipient's e-mail address (substitute your own):
$to = "you@youremail.com";
Create a variable to hold the subject of the e-mail:
$subject = "Web Site Feedback";
Create a variable to hold additional mail headers:
$mailheaders = "From: My Web Site <genericaddress@yourdomain.com>\n";
| Note |
Mail headers are the strings at the beginning of mail messages that formulate their structure and essentially make them valid mail messages. |
Add the following to the $mailheaders variable:
$mailheaders .= "Reply-To: $_POST[sender_email]\n";
Add the mail() function:
mail($to, $subject, $msg, $mailheaders);
Close your PHP block:
?>
Although this code will send the mail, you should return something to the user's screen so that he knows the form has been sent. Otherwise, he might sit there and continually click the Send This Form button.
Start the HTML output:
<HTML> <HEAD> <TITLE>Simple Feedback Form Sent</TITLE> </HEAD> <BODY>
Add some information to tell the user what has happened:
<H1>The following e-mail has been sent:</H1>
Add the text label for the Your Name field and display the user's input, and do the same for the other fields:
<P><strong>Your Name:</strong><br> <? echo "$_POST[sender_name]"; ?> <P><strong>Your E-Mail Address:</strong><br> <? echo "$_POST[sender_email]"; ?> <P><strong>Message:</strong><br> <? echo "$_POST[message]"; ?>
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name send_simpleform.php, and place this file in the document root of your web server. Your code should look something like the following figure.
In the next section, you submit the form and see all these different types of string functions at work.
Now that you've created both a front-end form and a back-end script, it's time to try them out.
Open your web browser and type http://127.0.0.1/simple_form.html.
Type your name in the Your Name field.
Type your e-mail address in the Your E-Mail Address field.
Type the following message in the Message field:
PHP is so cool!
Click on the Send This Form button.
The information you entered, along with a confirmation that your e-mail has been sent, will appear.
Now check your e-mail, and see if a message is waiting for you.
Open send_simpleform.php in your text editor.
Modify the string containing Sender's Name by replacing the tab character (\t) with two spaces:
Modify the string containing Sender's E-Mail by replacing the tab character (\t) with four spaces:
$msg .= "Sender's E-Mail: $_POST[sender_email]\n";
Modify the string containing Message by replacing the tab character (\t) with 10 spaces:
$msg .= "Message: $_POST[message]\n";
Save the file and upload it to your server.
In the next section, you create custom error messages for when fields are blank, and you streamline the two-step process of sending mail into one cohesive script.