Forms on websites can be used for a lot of purposes besides being a place to contact the website administrators. In previous posts, I covered how to create a fully functional contact form in PHP and how to build your own Captcha and integrate it in your contact forms. In this tutorial, our focus will be on creating forms in PHP that are meant to serve other purposes like gathering information about a job applicant, booking hotel rooms or getting information about marriage events from clients.
Choosing the Right Fields for your Forms
In the contact form tutorial, we learned that different organizations and websites will most probably want to get specific information from people contacting them in order to assist them in the best possible way. For instance, a contact form on a school website might ask parents information about their kids enrolled in that school. Similarly, the contact form on a shopping website might gather information about the last product people bought in order to serve them better.
If contact forms can vary so much between different websites and organizations, it is reasonable to assume that forms which are supposed to serve completely different purposes have very different fields. For instance, you will most probably ask people booking a hotel room if they will be accompanied by any other adults or children. Asking the same thing of a job applicant would make no sense.
This is an important thing to remember when you follow this tutorial. The basic concept of creating a form, gathering all the information and sending it somewhere ,by email will stay the same but the code will almost certainly need changes to work for your case. You might want to add different input fields to your forms and assign different names to those fields. This will then affect the PHP code in the backend. If you make changes in the forms, make sure you update the PHP code as well.
You can take a look at this list of 20 Best PHP email forms to get some inspiration about the layout of form elements or different input fields that you might want to add to your forms.
-
PHP20 Best PHP Email Forms
Creating an HTML Form
In this tutorial, we will create a form to book hotel rooms as an example. For simplicity, we will also not use any additional JavaScript or PHP libraries.
With this in mind, lets write the markup of our form. We need to ask people making a reservation their name, email address and phone number. After that, we ask them to provide details about the number of children and adults that will accompany them, the check-in and check-out dates along with the types of room they prefer. In the end we will ask them if there is anything else that we show know before their arrival.
We have used two basic patterns to check if the provided name and phone numbers are valid. Phone numbers follow different formats depending on the locale. This means that you will have to update the value of pattern attribute accordingly.
We have also set the minimum value of adults and children to 1 and 0 respectively. This way we can prevent people from mistakenly adding negative values.
You can add more elements to this form for other services that the hotel provides like pickup, room service etc.
Right now, visitors can set both the check in and check out dates to a past value. We can avoid that by simply setting the value of min
attribute on both the check in and check out dates. In our case, we will set the minimum possible check in date to be the current day. The value of min
attribute of the check out date will be the check in date.
The JavaScript code below will handle all the logic for us. The months are zero-based so we add 1 to the returned value to get the actual month. The returned date value can be any integer from 1 to 31 depending on the current time.
When the date and month values are in single digits, they are padded with an extra 0 so that the final string stays in a valid format.
var currentDateTime = new Date(); var year = currentDateTime.getFullYear(); var month = (currentDateTime.getMonth() + 1); var date = currentDateTime.getDate(); if(date < 10) { date = '0' + date; } if(month < 10) { month = '0' + month; } var dateTomorrow = year + "-" + month + "-" + date; var checkinElem = document.querySelector("#checkin-date"); var checkoutElem = document.querySelector("#checkout-date"); checkinElem.setAttribute("min", dateTomorrow); checkinElem.onchange = function () { checkoutElem.setAttribute("min", this.value); }
The following CodePen demo shows the us how the form behaves after adding the above markup and JavaScript functionality.
Sending the Form Data by Email with PHP
The final step involves collecting all the data from our form and sending it to concerned people in an email. This is actually very easy to do. First we gather all the information using $_POST
variables. Once we have the required information we sanitize it to remove anything malicious. Finally, we create the body of our HTML email and then send it using the mail() function in PHP.
The following code will go into a file called reservation.php. The file name will be different if you changed the value of action
attribute of the from in previous section.
"; $email_content .= "
Visitor Name | $visitor_name |
Visitor Email | $visitor_email |
Visitor Phone | $visitor_phone |
Total Adults | $total_adults |
Total Children | $total_children |
Check-in Date | $checkin_date |
Check-out Date | $checkout_date |
Room Type | $room_type |
Special Request from $visitor_name — $visitor_message.
"; $email_content .= '