One of the easiest ways to get people to contact you is by creating a contact form. If you haven't used a contact form in a while, click on the screenshot below to try mine out:
The following diagram should help set the stage:
You basically have three files: the form, PHP script, confirmation page. Someone fills out the form and hits Send. When the Send button is hit, the data from your form is transmitted to a PHP script, and this script formats the data in a way that can be mailed. Once your data has been mailed, the PHP script loads a confirmation page letting the form submitter know that the data has been sent.
That's all there is to it, but let's go a little bit deeper. The form contains the various UI elements that you use to capture and submit the data - the textboxes, textarea, and send button:
When you press the send button, your data goes to a PHP script that lives in its own page:
This script that takes care of processing your data and e-mailing it to you. This script also contains some logic for displaying a confirmation page once your form data gets sent:
In the next few sections, you will create and populate all three of these pages. The first goal is to get you quickly up and running with a contact form. Once you have done that, we'll go back and look at the details of why everything looks and functions the way it does.
On your web server, you will need to create two HTML files and one PHP file:
Once you have created these files, you'll fill them up with the content I will provide.
To get the most out of this tutorial, I would suggest you know some basic HTML and CSS techniques such as defining content and styling them. The CSS Tutorials section of this site will get you up to speed if you aren't too familiar with them.
I've mentioned that this example uses PHP a few times to actually send the e-mail out. You do not need any PHP knowledge to make all of this work. I will provide the code, specify where to put your e-mail address, and then provide a basic explanation of why the code works the way it does.
Ok, onwards!
If you preview the document in your browser, you will see a contact form that should look identical to the following screenshot:
Don't click on the Send button just yet. If you click the Send button, you will find that nothing actually happens. We'll fix that shortly...next to be precise!
You are almost done.
And with that, you have finished adding content to your HTML and PHP pages.
Right now, if you preview your contact form, fill it out, and press Send, you will see the above confirmation page displayed in your browser.
More importantly, an e-mail will be sent to the address you specified in mailer.php that will look similar to the following image:
The e-mail doesn't look pretty, but it does contain the feedback that I filled out and sent!
The form elements are contained inside the form tag:
So, we have a means of sending the data, but you may be wondering what data do we actually send? The data that needs to get sent is determined by associating your form elements' names with their values.
In our example, the names for our form elements are highlighted in yellow below:
When you press the Send button, your browser takes all of these name/value pairs and passes them along to mailer.php. Since our form has an input element of type submit, clicking it will automatically cause your form to do what its action and method attributes specify. In our case, that means sending all of the data to mailer.php.
One thing to note is that there is no JavaScript or additional code needed to make this all work. This functionality is part of the logic built-in to your browser to handle forms.
It is actually not as complicated as it sounds. Here is the code that you currently have:
The next two lines are pretty straightforward:
Things are starting to get interesting now:
Instead, the values consist of the form data which I associated based on the name value I gave the form elements inside contactform.htm. In other words, the names of our various form elements (name, email, comment) are provided as the key that points to the actual data they store:
In the next line, we construct our message:
Once you have your e-mail message, it is time to send it off:
And now...the last line:
Conclusion
Well, that's all there is to this tutorial. One of the most difficult things to wrap your head around is the interaction between your HTML page and the server-side PHP script. Once you get a hang of that, creating a contact form will be one of the easiest things you will do.
[ go ahead - try it out! ]
By the end of this tutorial, you will have learned how to create a simple contact form (just like the one you see above) that uses a little bit of PHP to send the form's data directly to your e-mail address.Anatomy of this Contact Form
Before we start diving into the HTML, CSS, and PHP to create the contact form, let's look at a high level how data from your form finds its way into your e-mail inbox.The following diagram should help set the stage:
That's all there is to it, but let's go a little bit deeper. The form contains the various UI elements that you use to capture and submit the data - the textboxes, textarea, and send button:
Getting Started
For this tutorial, you need to have a web server capable of running PHP. If you do not have such a server, give mediatemple a shot. They sponsor the hosting for this site, and they are awesome!On your web server, you will need to create two HTML files and one PHP file:
- contactform.htm
- confirmation.htm
- mailer.php
To get the most out of this tutorial, I would suggest you know some basic HTML and CSS techniques such as defining content and styling them. The CSS Tutorials section of this site will get you up to speed if you aren't too familiar with them.
I've mentioned that this example uses PHP a few times to actually send the e-mail out. You do not need any PHP knowledge to make all of this work. I will provide the code, specify where to put your e-mail address, and then provide a basic explanation of why the code works the way it does.
Ok, onwards!
Creating the Contact Form
Open contactform.htm and add the following HTML and CSS to your document:- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"><title>What are you thinking?</title>
- <meta content="php, contact, form, thinking" name="keywords">
- <meta content="Contact us and let us know if we can help you out further." name="description">
- <style>
- input, textarea {
- padding: 5px;
- margin: 10px;
- font-family: Cambria, Cochin, serif;
- font-size: medium;
- font-weight: bold;
- outline: none;
- }
- p {
- font-family: Cambria, Cochin, serif;
- font-size: large;
- margin-bottom: -5px;
- }
- input[type=text], textarea {
- width: 350px;
- background-color: #DDEDFF;
- border: 1px solid #97C9FF;
- }
- input[type=submit] {
- width: 100px;
- background-color: #669900;
- border: 1px solid #336600;
- font-size: large;
- color: #FFFFFF;
- }
- input[type=submit]:hover {
- background-color: #78B300;
- cursor: pointer;
- }
- input[type=submit]:active {
- background-color: #4A6F00;
- }
- h1 {
- font-family: "Trebuchet MS", Arial, sans-serif;
- font-size: 2.1em;
- color: #3399FF;
- }
- body {
- padding: 10px;
- background-color: #F4F4F4;
- }
- </style>
- </head>
- <body>
- <h1>What are you thinking?</h1>
- <form action="mailer.php" method="POST">
- <div>
- <p>Name</p>
- <input name="name" type="text"> <br> </div>
- <div>
- <p>E-Mail (Optional)</p>
- <input name="email" type="text">
- <br>
- </div>
- <div>
- <p>Comment</p>
- <textarea cols="30" name="comment" rows="9"></textarea>
- <br> </div>
- <div>
- <input name="submit" type="submit" value="Send!"> </div>
- </form>
- </body>
- </html>
If you preview the document in your browser, you will see a contact form that should look identical to the following screenshot:
Adding the PHP Code
The next thing we will do is add the PHP code needed to make our form actually work. Open mailer.php and add the following code:- <?php
- if(isset($_POST['submit'])) {
- $to = "you@email.com";
- $subject = "What are you thinking submission!";
- // data the visitor provided
- $name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
- $email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
- $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
- //constructing the message
- $body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
- // ...and away we go!
- mail($to, $subject, $body);
- // redirect to confirmation
- header('Location: confirmation.htm');
- } else {
- // handle the error somehow
- }
- ?>
- $to = "you@email.com";
You are almost done.
The Confirmation Page
The last thing we are going to do is add our confirmation page. Open confirmation.htm and add the following code to it:- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Great Success!</title>
- <meta content="php, contact, form, thinking" name="keywords">
- <meta content="Great success!" name="description">
- <style>
- p {
- font-family: Cambria, Cochin, serif;
- font-size: large;
- margin-bottom: -5px;
- }
- h1 {
- font-family: "Trebuchet MS", Arial, sans-serif;
- font-size: xx-large;
- color: #3399FF;
- }
- body {
- padding: 10px;
- background-color: #F4F4F4;
- }
- </style>
- </head>
- <body>
- <h1> </h1>
- <h1>Thank You!</h1>
- <p>We've received your feedback, and we will get back to you soon.</p>
- </body>
- </html>
Right now, if you preview your contact form, fill it out, and press Send, you will see the above confirmation page displayed in your browser.
More importantly, an e-mail will be sent to the address you specified in mailer.php that will look similar to the following image:
Behind the Scenes
Right now, you have a working example that you created by largely copying and pasting code that I provided. In the next couple of sections, let's take a deeper look at what you have done so that you have an understanding of why your contact form works the way it does.Looking at the Form Itself
Our contact form (contactform.htm) is made up of nothing but standard HTML and CSS tags. I am going to defer on the styling aspects of things, but instead, let's look at the form elements and what they contain that helps your PHP script know what to send via e-mail.The form elements are contained inside the form tag:
- <form action="mailer.php" method="POST">
- <div>
- <p>Name</p>
- <input name="name" type="text"> <br> </div>
- <div>
- <p>E-Mail (Optional)</p>
- <input name="email" type="text">
- <br>
- </div>
- <div>
- <p>Comment</p>
- <textarea cols="30" name="comment" rows="9"></textarea>
- <br> </div>
- <div>
- <input name="submit" type="submit" value="Send!"> </div>
- </form>
- <form action="mailer.php" method="POST">
So, we have a means of sending the data, but you may be wondering what data do we actually send? The data that needs to get sent is determined by associating your form elements' names with their values.
In our example, the names for our form elements are highlighted in yellow below:
- <form action="mailer.php" method="POST">
- <div>
- <p>Name</p>
- <input name="name" type="text"> <br> </div>
- <div>
- <p>E-Mail (Optional)</p>
- <input name="email" type="text">
- <br>
- </div>
- <div>
- <p>Comment</p>
- <textarea cols="30" name="comment" rows="9"></textarea>
- <br> </div>
- <div>
- <input name="submit" type="submit" value="Send!"> </div>
- </form>
[ the value is what you actually provide the form element with ]
Taking the above example, the value of our text field named name would be Kirupa.When you press the Send button, your browser takes all of these name/value pairs and passes them along to mailer.php. Since our form has an input element of type submit, clicking it will automatically cause your form to do what its action and method attributes specify. In our case, that means sending all of the data to mailer.php.
One thing to note is that there is no JavaScript or additional code needed to make this all work. This functionality is part of the logic built-in to your browser to handle forms.
Looking at mailer.php
The real magic happens inside the mailer.php file where you have some PHP code that takes your form's input and sends it out via e-mail to an address you specify.It is actually not as complicated as it sounds. Here is the code that you currently have:
- <?php
- if(isset($_POST['submit'])) {
- $to = "you@email.com";
- $subject = "What are you thinking submission!";
- // data the visitor provided
- $name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
- $email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
- $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
- //constructing the message
- $body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
- // ...and away we go!
- mail($to, $subject, $body);
- // redirect to confirmation
- header('Location: confirmation.htm');
- } else {
- // handle the error somehow
- }
- ?>
- if(isset($_POST['submit'])) {
The next two lines are pretty straightforward:
- $to = "you@email.com";
- $subject = "What are you thinking submission!";
Things are starting to get interesting now:
- // data the visitor provided
- $name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
- $email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
- $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
Instead, the values consist of the form data which I associated based on the name value I gave the form elements inside contactform.htm. In other words, the names of our various form elements (name, email, comment) are provided as the key that points to the actual data they store:
- // data the visitor provided
- $name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
- $email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
- $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
In the next line, we construct our message:
- //constructing the message
- $body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
Once you have your e-mail message, it is time to send it off:
- // ...and away we go!
- mail($to, $subject, $body);
- The e-mail address you want to send the mail to
- The subject of your e-mail
- The contents of your e-mail
And now...the last line:
- // redirect to confirmation
- header('Location: confirmation.htm');
Conclusion
Well, that's all there is to this tutorial. One of the most difficult things to wrap your head around is the interaction between your HTML page and the server-side PHP script. Once you get a hang of that, creating a contact form will be one of the easiest things you will do.
0 comments
একটি মন্তব্য পোস্ট করুন