PHP
Contact Form - The PHP Code
         by kirupa : 20 December 2004

In the previous page, you created the contact.htm file. Of course, that is just one half of our contact form. The PHP file, which you'll create on this page, will complete our basic contact form!

So, here is our PHP code. You should place the following code into a new file you create called mailer.php.

<?php
if(isset($_POST['submit'])) {
$to = "you@you.com";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
 
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
 
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>

Once you copy and paste the above code into mailer.php, make sure you change the text you@you.com to your e-mail address. Save this file.

You should now have completed versions of both the contact.htm and mailer.php files. Upload both of those files to the same directory on your web server. Open the contact.htm page in your browser. Fill out the form and press the Submit button. If everything worked out, which I'm sure it did, you should receive the data you entered in the mailbox of the address you specified in mailer.php.


Why this Works
In this and the previous page, you simply copied and pasted code I gave you. I'm going to explain why the code works so that you can implement your own variation of this form for your needs later!

Let's start with the HTML code:

<form method="POST" action="mailer.php">
   <input type="text" name="name" size="19"><br>
   <br>
   <input type="text" name="email" size="19"><br>
   <br>
   <textarea rows="9" name="message" cols="30"></textarea>
   <br>
   <br>
   <input type="submit" value="Submit" name="submit">
</form>

The first line of code tells the form where to send the data. In our case, our form data will be handled by our php file mailer.php. The word post is emphasized because we are telling the form to send the data to the file.

The next sections of code are standard HTML definitions for creating form elements. The important thing you should take note are the actual names I have given the form elements. The input text fields are called name and email. The name of the message box is called message. Finally, your submit button is aptly called - submit!

The names of the form elements have no effect on the functioning of your form. They simply help to categorize the data, and that will be useful when we are retrieving the data into the PHP file!

Let's now look at the PHP code:

<?php
if(isset($_POST['submit'])) {
$to = "you@you.com";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
 
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
 
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>

In this section of code, I check to see if the visitor accessed mailer.php via contact.htm or if the visitor simply went to the mailer.php file directly. If the user pressed the Submit button on contact.htm, the if statement will return a true because isset($_POST['submit']) will not return false!

If someone accessed the site without first pressing the Submit button on contact.htm, isset($_POST['submit']) will return false and the code under else would be executed.

$to = "you@you.com";
$subject = "Form Tutorial";

In the next two lines of your PHP code, you create two variables that simply store your e-mail address and the e-mail subject. Nothing too complicated here.

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

Finally - some action! These three variables catch the data sent from the form on contact.htm. The form data is collected in a series of $_POST variables with the form element name specified. Note the appearance of name, email, and message...the same three names we gave our form elements in our contact.htm page earlier.

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

In the above line I am just combining all previous parts of our message into one variable. Why would I want to do that?

The reason is that the PHP mail function only takes in one variable for the body of your e-mail. Since we have three variables relating to information the user submitted, we need to nicely compact all three pieces of data into one variable that can be passed into our mail function. You will see what I mean when I explain my use of the mail function below.

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

This is the section of code that executes if the user submitted the form data. The first line simply displays a line of text in the browser that says "Data has been submitted to you@you.com!" (where you@you.com is your e-mail address specified by $to).

The second line is the PHP mail function. The mail function takes in three arguments. It takes the address to send the data to, the subject of the e-mail, and the body of your e-mail. If you remember, earlier I created a variable $body that took and formatted the values of what the user sent. I gave a weak reason without any proof as to why I would need to do that. Now you see that the mail function only takes in one argument for the body of the e-mail message. That's why I, as I explained earlier, combined the user inputted data into the $body variable. That's the only way our mail function would display all of the user-inputted data.

echo "error: no data sent!";
The above line executes only when the user accesses the mailer.php file without actually submitting the form from contact.htm. In that case, the browser simply displays the no data sent message.

In the next page, now that you have a basic understanding of how a simple form and php file cooperate to send data to your mailbox, I will expand upon what we have created and introduce some other form elements that you may find useful!

Onwards to the next page!


page 2 of 4


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.