$99 Websites + Website Design 5505 Sun Harbor Road Panama City, Florida 32401 850-329-3425 http://website99.us https://www.google.com/maps/place/$99+Websites+%2B+Website+Design/@30.1842909,-85.7322502,15z/data=!4m5!3m4!1s0x0:0x61b35c1bb4fa6eac!8m2!3d30.1842909!4d-85.7322502
Monday, December 31, 2018
Why Having an 'Internal Resume' Is Just As Important As Having an External One
7 Steps to Help You Look for the Lesson or the Gift in a Mistake or Hardship
All the Predictions for 2019 Are Utter Nonsense
How Long Does It Take You to Decide Whether Someone is Attractive? Science Says Just 3 Seconds
What Elon Musk Got Wrong With Artificial Intelligence
10 Best Albums of 2018 for People Who Actually Like Music
Insane But True: This Is the 1 Night of the Year It's OK to Lie to Your Kids. Here's How
How to Use Persuasion to Improve Your Leadership Potential
60 Inspiring Quotes About Writing from the World's Greatest Authors
There's an Enormous Trend on the Horizon that Could Completely Change Cloud Computing
Instagram Testing Error Shows Likely Future of the Platform
The 5 Biggest Leadership Trends to Watch in 2019
Why Google, Apple, and Microsoft Are On Board With This Progressive Trend in 2019
4 Reasons Why Entrepreneurs Struggle to Step Up their Game--And How They Can Crush 2019
The 3 Most Beneficial Books for Entrepreneurs (That Go Beyond Teaching You How to Run a Business)
Yale Psychologists: Introverts Are Better Than Extroverts at Performing This Essential Leadership Skill
Netflix Just Made a Truly Stunning Announcement and People Are Now Staring in Disbelief
A Brutal Truth About Keeping New Year's Resolutions That Few People Are Willing to Admit
Why You Need to Shake Things Up at Your Company--and How to Start
Happy New Year. Now Stop Trying to Make Everyone Happy.
Why Dreaming While You Sleep Is a Good Sign for Your Job Performance
Investing in Employees Adds No Value to Your Company--Technically. 7 Strategies to Make Sure it Pays Off
Drinking Champagne Tonight? Don't Make These Common Mistakes
The Minimum Wage Increases in 20 States and 24 Cities Tomorrow. Is Yours One of Them?
In-N-Out Just Made an Announcement That's Going To Frustrate a Lot of People
How to Handle a Non-Drinker at the Office
19 Powerfully Inspiring New Year Quotes to Start 2019 Off Right
60 Years Ago, Walt Disney Taught a Major Lesson in Emotional Intelligence. Here It Is in 1 Sentence
How This Octogenarian Turned Defective Diapers and Scrap Paper Into a Family Business for the Ages
Here Are the Best Inspirational Quotes for 2019
Sunday, December 30, 2018
21 Quotes About Entrepreneurship That'll Make You Think (and Some That'll Make You Laugh)
4 Emotionally Intelligent Skills That Will Transform How You Handle Tense Conversations
3 Smart Ways to Market Travel to Millennials in 2019
Start the New Year Right With These 3 Email Tips
One Simple Way to Experience Less Drama in 2019
7 Quick (and Free) Ways to Find Inspiration and Be More Creative in 2019
The Travel & Hospitality Industry is Being Disrupted, Again.
I Tried Getting Up At 4 a.m. To Be More Successful. It Was Hell
You Have More Time Than You Think To Make a Good First Impression, Says a New Study
A Top American Airlines Executive Flew Delta. Her Review is Shocking
3 New Year's Resolutions Successful People Make (and Keep)
2 Simple Tweaks That Will Vastly Improve Your Social Life in 2019
Can You Tell the Difference Between a Human Face and an AI-Created Fake? Take This Test to Find Out
Here's How Apple Lost $9 Billion After President Trump's 'Last Minute Tax Law' (It's Insane, But Also Kind of Funny, Sorry)
Your Guide to an Alternative, Way-More-Fun New Year's Eve Celebration
Saturday, December 29, 2018
The 10 Most Googled People of 2018 (Who'd You Look Up?)
American Airlines Just Made Some Very Famous People Angry. Here's Why
3 Ways to Share Expectations for Your Employees Really Effectively
The Many Benefits of Licensing Your Hit Idea to a Market Leader
25+ Google Tricks (Guaranteed You Won't Know All of Them!)
Maximize Your Productivity in the New Year With These 3 Tips
The Gender Pay Gap Doesn't Have to Exist. Here's What You Can Do Today to Help Fix It
How #MeToo Concerns Made Me Nervous at a Turkish Dance
Is This the World's Worst Airline Passenger?
CenturyLink Outage Disrupts 911 Services So Company Tweets: Drive to Fire Station for Help
A Delta Air Lines Flight Attendant's Dad Gave Her a Truly Stunning Gift. But How Did Everyone Miss the Other Big Hero In This Viral Story?
I Just Flew Cross-Country on United's Newest, Most Uncomfortable Plane. It Hurt
When Procter & Gamble Cut This, Their Profits Went Up by 10%
The Secret to a Longer Life Lies in 3 Things You're Told To Avoid, Says an Eye-Opening 15-Year Study
United Airlines Says It's Doing an 'Unforgettable' Thing For All 25,000 United Flight Attendants. But Will Passengers Like the Result?
According to This Truly Surprising New Study, You Have Just 27 Seconds to Make a First Impression
How to Develop the Grit You Need to Succeed
These Amazon Packages Were Stolen. What Happened Next Was Truly Shocking
Here's How to Make New Year's Resolutions That Will Actually Stick
Got a McDonald's or Burger King Coupon? Here's the Smart, Surprising Thing to Do With It. (You Only Have 3 Days)
Elon Musk Adds Larry Ellison to Tesla's Board, Fulfilling SEC Requirement--Sort of
Friday, December 28, 2018
4 Simple Habits That Will Make You More Likable Right Away
Sears Just Won a Long-Shot Chance at Survival. The 125-Year-Old Retailer Lives On--For Now
This Evening of Food Theater Goes Beyond Inspiring Innovation. It's a Master Class in the Art of Possibility
Aliens, Trolls, Morality, and Google: The Top 10 TED Talks of 2018
I Travel More Than 100,000 Miles a Year. Here Are My Best Tips for Saving Money on Hidden Fees
5 Tips to Manage Low Humidity When Flying
Success Recipe: Positive Role Models, More Office Privacy, and a Great Cup of Coffee
The 7 Best Shows to Binge Watch for Students of Leadership
Why 'Work Wives' and 'Office Besties' Are Hurting Your Team's Productivity
Make Meaningful Resolutions With This Effective Technique
Employees Quitting? According to LinkedIn's Jeff Weiner, That's Your Fault
Here's the Right Way to Conduct a Review of the Prior Year
I Played 'Monopoly for Millennials' With a Few Young Adults. The Results Were Totally Surreal
2 Simple Ways to Be More Peaceful in 2019
Your Guide to TSA PreCheck and Global Entry for Business Travels
Grab Your Best Highlighter and Read These 2 Books
Leaders, This Is How You De-Stress For 2019
Resolution Hacking: 10 Podcasts to Keep New Year's Goals on Track
Will Smith Says 1 Thing Separates People Who Achieve Their New Year's Resolutions From Those Who Only Dream
How to Properly Allocate Your Marketing Budget in 2019
How This MIT Graduate Started a Software Business and Closed Jet.com in Less Than 18 Months
How to Use Humor to Win Your Customers' Hearts
Create a Contact Form in PHP
No matter what type of website that someone owns or manages, they probably need a contact form. The contact form can help your visitors request a quote, ask for information, or share any tips or problems that they are facing while using a website.
In this tutorial, our focus will be on creating a fully functional contact form in PHP from beginning to end. We will begin with the markup of all the fields that we need to add and the basic styling of the contact form. After that, we will move on to the PHP code to implement its functionality.
Of course, the easiest way to create a contact form is to download a professional contact form script from CodeCanyon.
But if you want to learn about how a contact form is created, read on! It might just be easier than you think.
Markup of Our HTML Contact Form
The first step towards creating our own contact form is to code the markup. We will start doing that once we have a list of all the elements that we want inside our form. We'll need an input field for the name of the person who is contacting us, and we'll need a field for their email address so that we can send a reply back to them if the need arises. We'll also need an input field for the reason people are contacting you and a textarea
where users can type their message.
If the website you are managing is very popular, you'll be getting a lot of emails through the contact form. To make sure that the right people get to read those emails and respond quickly, you need a couple more fields. For instance, you could add a field that can determine which department the visitor wants to contact like marketing, support, or billing. This information can later be used to route the email to the appropriately. Ultimately, that might help you reply back more quickly and sort the emails more efficiently.
How many fields you add to the contact form depends on the type of website you run but make sure you don't over do it. Forcing visitors to fill out too many details might discourage them from contacting you altogether.
Lets write the HTML code to add all the fields I just mentioned into our contact form.
<form action="contact.php" method="post"> <div class="elem-group"> <label for="name">Your Name</label> <input type="text" id="name" name="visitor_name" placeholder="John Doe" pattern=[A-Z\sa-z]{3,20} required> </div> <div class="elem-group"> <label for="email">Your E-mail</label> <input type="email" id="email" name="visitor_email" placeholder="john.doe@email.com" required> </div> <div class="elem-group"> <label for="department-selection">Choose Concerned Department</label> <select id="department-selection" name="concerned_department" required> <option value="">Select a Department</option> <option value="billing">Billing</option> <option value="marketing">Marketing</option> <option value="technical support">Technical Support</option> </select> </div> <div class="elem-group"> <label for="title">Reason For Contacting Us</label> <input type="text" id="title" name="email_title" required placeholder="Unable to Reset my Password" pattern=[A-Za-z0-9\s]{8,60}> </div> <div class="elem-group"> <label for="message">Write your message</label> <textarea id="message" name="visitor_message" placeholder="Say whatever you want." required></textarea> </div> <button type="submit">Send Message</button> </form>
Before proceeding any further, I would like to quickly summarize the meaning of some important attributes in the above markup. The action
attribute in the form determines where the form data needs to be sent. If you don't have an action
attribute, the data is sent back to the same URL. Here we've used contact.php, so the form data will be sent to that script.
The name
attribute for different input elements in the form is used to access the element values on the server side. For example, in the above form, you can get the name of the visitor contacting you using $_POST['visitor_name']
in contact.php.
We use the placeholder
attribute to give users some basic idea of the expected input for each field in the form. The required
attribute ensures that no important field is left blank before the user hits the submit button on the form. The pattern
attribute is used to enforce some rules on the kind of values that can go inside certain fields. In our case, we only allow users to use letters and the space character in the names they submit. We also limit the total number of acceptable characters from 3 to 20 inclusive. The pattern that you use will depend on the type of input that you want from users.
The following CodePen demo shows us what our contact form looks like with the above markup and a little bit of CSS.
Making Our HTML Contact Form Functional Using PHP
Right now, our form doesn't do anything useful. Visitors can fill it out and hit the send message button but we won't receive anything because there is no serve- side code to handle the information provided by the form. In this section, we'll make our contact form functional using PHP.
Begin by creating a contact.php file and put the following code inside it.
<?php if($_POST) { $visitor_name = ""; $visitor_email = ""; $email_title = ""; $concerned_department = ""; $visitor_message = ""; if(isset($_POST['visitor_name'])) { $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING); } if(isset($_POST['visitor_email'])) { $visitor_email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['visitor_email']); $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL); } if(isset($_POST['email_title'])) { $email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING); } if(isset($_POST['concerned_department'])) { $concerned_department = filter_var($_POST['concerned_department'], FILTER_SANITIZE_STRING); } if(isset($_POST['visitor_message'])) { $visitor_message = htmlspecialchars($_POST['visitor_message']); } if($concerned_department == "billing") { $recipient = "billing@domain.com"; } else if($concerned_department == "marketing") { $recipient = "marketing@domain.com"; } else if($concerned_department == "technical support") { $recipient = "tech.support@domain.com"; } else { $recipient = "contact@domain.com"; } $headers = 'MIME-Version: 1.0' . "\r\n" .'Content-type: text/html; charset=utf-8' . "\r\n" .'From: ' . $visitor_email . "\r\n"; if(mail($recipient, $email_title, $visitor_message, $headers)) { echo "<p>Thank you for contacting us, $visitor_name. You will get a reply within 24 hours.</p>"; } else { echo '<p>We are sorry but the email did not go through.</p>'; } } else { echo '<p>Something went wrong</p>'; } ?>
We have already done some client side validation of user input. However, it is always safer to do server side validation as well. We use the filter_var()
function to sanitize the name provided by the user. In a similar fashion, we also sanitize the value of $email_title
and $concerned_department
. You can use the filter_var()
function to validate or sanitize all types of user input. We also use the htmlspecialchars()
function to encode all the special HTML characters in the visitor message sent to us.
The value of $recipient
is based on the value of the variable $concerned_department
. This way we make sure that only people who are actually supposed to look into the matter receive the email.
Finally, we use the mail()
function to send an email which includes the information the visitor wanted us to know. Upon successful delivery of the email, we let the visitors know that we have received their email and that they will be contacted soon.
Security is paramount when you are dealing with user data or input. Whether you should validate or sanitize the user input depends on what the input is and how you want to use it. Validation simply checks if the user input follows a certain set of rules. For example, validation could check that the name of a person does not contain any numbers. Sanitization is used to remove any offending characters that pose a security risk. For example, a malicious user trying to contact you through the form might add a script
tag in the textarea
to get you to download a harmful script. This is particularly worrisome when your website has public forums accessible by everyone.
However, you have to be very careful when getting rid of unwanted characters in user input. For example, you might decide to use filter_var($user_input, FILTER_SANITIZE_STRING);
on some input to strip all tags and encode special characters. However, this flag also strips harmless character input by legitimate users. Here is an example:
<?php $string = 'One of your posts about inequalities mentioned that when x < y and y < z then x < z.'; // Output: One of your posts about inequalities mentioned that when x echo filter_var($string, FILTER_SANITIZE_STRING); // Output: One of your posts about inequalities mentioned that when x < y and y < z then x < z. echo htmlspecialchars($string); ?>
If your website has a lot of maths related topics, it will be relatively common for users to write < or > in contact forms or forum posts. Using the FILTER_SANITIZE_STRING
flag with the filter_var()
function will strip necessary information from the message in this case.
The point I am trying to make is that even though you should always validate or sanitize user data, make sure that you are not stripping away crucial information in the process.
Final Thoughts
Creating a basic contact form in PHP is relatively simple once you are familiar with the basics. You begin by writing the HTML needed to create input elements for information like the users name, email address, phone number etc. Next step is writing CSS to make sure the contact form blends in perfectly with the rest of the website. The final step involves writing the PHP code that will take the information from the contact form and securely mail it to you.
The aim is to make sure that different form elements are laid out in a way that does not confuse people and the user input is sanitized and validated before you mail it to concerned parties. If all this is new to you or if you don't want to spend a lot of time creating a professional looking contact form, you should definitely check out these top rated contact form PHP scripts.
If you have any questions, feel free to let me know in the comments. My next tutorial will show you how to implement your own CAPTCHA to help keep contact form spam in check.
If you're interested in professional-quality PHP form scripts that you can start using on your site today, check out some of our other posts.