1. Code
  2. JavaScript
  3. jQuery

How to Create a Web Booking Form With the Smart Forms Script

Scroll to top

Booking forms can serve a lot of purposes on a website. You can use them to allow your visitors to book a hotel room or tickets to a popular event. They can also be used for other things like booking appointments with clients.

They are a great way for people to simply give you all the necessary information about their stay or visit and check the availability on a particular date. This saves both you and your clients a lot of hassle.

There are a lot of plugins and scripts available for you to add booking forms to your website. The right choice for your situation depends on the features that you are looking for as well as factors like ease of use and budget. In this tutorial, I will show you how to create a booking form for your website using the Smart Forms script from CodeCanyon (no longer available).

Why Use Smart Forms?

Before we start diving deeper into the topic, I would like to explain why I chose Smart Forms for this task.

One of the first things that you will note on the description page of Smart Forms is that it offers a lot of features and allows you to create all kinds of forms. There is a good chance that the website you are creating will need at least a few other different types of forms, such as a contact form or a comment form. With Smart Forms, you will be able to create all of these in very little time.

You also get lots of options when it comes to customization and features. The script is updated on a regular basis and keeps receiving new features. It also offers a large number of starter templates with different forms in three unique styles and color schemes. This way you can be certain that any form you create blends in perfectly with the rest of your webpage.

What We'll Be Building

Let's create the front-end of a booking form using Smart Forms. We will be creating a form to book hotel rooms. The same principles will be applicable for creating a booking form for an event or any other type of form. You just have to make the appropriate changes to the input fields.

Here's what we'll be making:

Room reservation formRoom reservation formRoom reservation form

Getting Started

If you haven't already, the first step would be to purchase the script from CodeCanyon. You will be able to download the files after you purchase the script, and you'll also be entitled to free lifetime updates and support for six months.

After you extract the downloaded .zip file, you will notice that it already contains a lot of templates for everything ranging from simple comment and contact forms to some multi-step forms. You can simply start using any of these templates in your projects and be good to go in ten to fifteen minutes.

To help you understand all the basics, we will be creating our booking form from scratch.

Creating the Booking Form

The markup of the booking form page should look something like this in the beginning.

1
<!DOCTYPE html>
2
<html lang="en">
3
  <head>
4
    <title> Booking Form - XYZ Hotels </title>
5
	<meta charset="utf-8">
6
	<meta name="viewport" content="width=device-width, initial-scale=1">
7
	<link rel="stylesheet" href="path/to/css/smart-forms.css">
8
	<link rel="stylesheet" href="path/to/css/font-awesome.min.css">	   
9
  </head>
10
  <body>
11
 
12
	<!-- OUR BOOKING FORM MARKUP -->
13
 
14
  </body>
15
</html>

Smart Forms relies on Font Awesome to add all the icons to the form, so you have to load both the CSS files.

Now, we will move on to the contents of our booking form. The Smart Forms framework divides the form into three sections: the form header, the form body, and the form footer.

The form header section at the top is used to display the form title in a presentable manner. For a booking form, you could say something like Book a room online or Make reservations in minutes.

The form body will contain a bunch of elements that visitors have filled out. You could ask for any relevant information here, like the number of adult guests and the number of children accompanying them. Similarly, you could ask for their period of stay, contact information, and other necessary details.

The form footer section contains the buttons to submit or cancel the form. It is displayed prominently at the bottom of the form.

Add the Smart Form Boilerplate

Here is the form markup that we need to add inside the body of our webpage to display our booking form.

1
<div class="smart-wrap">
2
  <div class="smart-forms smart-container wrap-2">
3
  
4
    <div class="form-header header-primary">
5
      <h4><i class="fa fa-calendar"></i>Make Room Reservations in Minutes</h4>
6
    </div>
7
    
8
    <form method="post" action="/" id="booking-form">
9
      <div class="form-body">
10
      
11
       <!-- OUR FORM ELEMENTS WILL GO HERE -->                                                
12
        
13
      </div>
14
      <div class="form-footer">
15
        <button type="submit" class="button btn-primary">Confirm Booking</button>
16
        <button type="reset" class="button">Cancel</button>
17
      </div>
18
      
19
    </form>
20
    
21
  </div>
22
</div>

With all the markup that we have added to our page so far, you should get something similar to the image below.

Booking Form Header and FooterBooking Form Header and FooterBooking Form Header and Footer

Building Out the Form Components

Let's add our actual form elements to the booking form now. This is the markup that you need to do so. It will go inside the div tag with class form-body.

1
<div class="section">
2
  <label for="guestname" class="field-label">Please Enter Your Name</label>
3
  <label for="guestname" class="field prepend-icon">
4
    <input type="text" name="guestname" id="guestname" class="gui-input" required="" placeholder="John Doe/Jane Doe">
5
    <span class="field-icon"><i class="fa fa-user"></i></span>  
6
  </label>
7
</div>
8
              
9
<div class="frm-row">
10
  <div class="section colm colm6">
11
    <label for="guestemail" class="field-label">Email Address</label>
12
    <label for="guestemail" class="field prepend-icon">
13
      <input type="email" name="guestemail" id="guestemail" class="gui-input" required="" placeholder="john@something.com">
14
      <span class="field-icon"><i class="fa fa-envelope"></i></span>  
15
    </label>
16
  </div>
17
  
18
  <div class="section colm colm6">
19
    <label for="guestelephone" class="field-label">Telephone / Mobile</label>
20
    <label for="guestelephone" class="field prepend-icon">
21
      <input type="text" name="guestelephone" id="guestelephone" class="gui-input" required="" placeholder="Telephone / Moble Number">
22
      <span class="field-icon"><i class="fa fa-phone-square"></i></span>  
23
    </label>
24
  </div>
25
</div>
26
27
<div class="frm-row">
28
  <div class="section colm colm6">
29
    <label for="adults" class="field-label">Number of Adults</label>
30
    <label for="adults" class="field prepend-icon">
31
      <input type="number" id="adults" name="adults" class="gui-input" required="" placeholder="Number of adults">
32
      <span class="field-icon"><i class="fa fa-users"></i></span>  
33
    </label>
34
  </div>
35
  
36
  <div class="section colm colm6">
37
    <label for="children" class="field-label">Number of Children</label>
38
    <label for="children" class="field prepend-icon">
39
      <input type="number" id="children" name="children" class="gui-input" required="" placeholder="Number of children">
40
      <span class="field-icon"><i class="fa fa-users"></i></span>  
41
    </label>
42
  </div>
43
</div>
44
45
<div class="frm-row">
46
  <div class="section colm colm6">
47
    <label for="checkin" class="field-label">Check-in Date</label>
48
    <label for="checkin" class="field prepend-icon">
49
      <input type="text" id="checkin" name="checkin" class="gui-input" required="" placeholder="mm/dd/yyyy">
50
      <span class="field-icon"><i class="fa fa-calendar"></i></span>  
51
    </label>
52
  </div>
53
  
54
  <div class="section colm colm6">
55
    <label for="checkout" class="field-label">Check-out Date</label>
56
    <label for="checkout" class="field prepend-icon">
57
      <input type="text" id="checkout" name="checkout" class="gui-input" required="" placeholder="mm/dd/yyyy">
58
      <span class="field-icon"><i class="fa fa-calendar"></i></span>  
59
    </label>
60
  </div>
61
</div>
62
63
<div class="spacer-t20 spacer-b30">
64
  <div class="tagline"><span>Please answer these questions for a pleasant stay</span></div>
65
</div>
66
67
<div class="frm-row">
68
  <div class="option-group field">
69
  
70
    <div class="section colm colm6">
71
      <label class="switch">
72
        <input type="checkbox" name="switch1" id="switch1" value="switch1">
73
        <span class="switch-label" data-on="YES" data-off="NO"></span>
74
        <span>Will you be bringing a pet?</span>
75
      </label>
76
    </div>
77
        
78
    <div class="section colm colm6">
79
      <label class="switch">
80
        <input type="checkbox" name="switch2" id="switch2" value="switch2">
81
        <span class="switch-label" data-on="YES" data-off="NO"></span>
82
        <span>Do you need us to pick you up?</span>
83
      </label>
84
    </div>
85
              
86
  </div>
87
</div>
88
89
90
91
<div class="section">
92
  <label for="comment" class="field-label">Anything else we should know about?</label>
93
  <label for="comment" class="field prepend-icon">
94
    <textarea class="gui-textarea" id="comment" name="comment" placeholder="Let us know about any special accommodation needs"></textarea>
95
    <span class="field-icon"><i class="fa fa-comments"></i></span>
96
    <span class="input-hint"> 
97
      <strong>Please:</strong> Be as descriptive as possible
98
    </span>   
99
  </label>
100
</div>

With all the markup added into the form, it should now look like the image below.

Complete Booking FormComplete Booking FormComplete Booking Form

It might seem like a lot of code now, but you can copy most of it from different starter templates. After that, you simply need to make little changes to the markup in order to customize it. For example, you can easily change the class of the Font Awesome icons for a more accurate representation of the input fields.

It is important to remember that you should be careful when making changes to some attributes of the form elements because this will affect the overall appearance or behavior of the form. For instance, you can change the width of different elements by changing the colm classes. When two input elements in a row have classes colm8 and colm4, their width would be in the ratio 2:1. If the digits don't add up to 12, you will also see some blank space in that row.

Whenever you change the value of the name attribute of a field on the front-end, make sure to change the corresponding values in the PHP files on the back-end. This way, you won't lose any form data that users have provided.

Adding Validation to Our Booking Form

The Smart Forms framework relies on core jQuery and many other jQuery-related plugins to give you different features like a month and time picker or the ability to drag and drop files. There are a total of 22 different JavaScript files that you can use to enhance the functionality of the form.

Make sure that you only include the files that are necessary for your particular form to work. The function of each of these files is mentioned in the documentation. In our case, we will need to include the core jQuery file, the jQuery UI file, and form validation files.

You can simply add the following code to your webpage to include all the JavaScript files. You can place it either in the head of the document or just before the end of the body.

1
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
2
<script type="text/javascript" src="js/jquery.form.min.js"></script>
3
<script type="text/javascript" src="js/jquery-ui-custom.min.js"></script>
4
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
5
<script type="text/javascript" src="js/additional-methods.min.js"></script>

Once you have added the files, you need to add some code that can begin the validation process or add other elements like a date range picker to the form UI. Here is the code that you need to add.

1
<script type="text/javascript">
2
$("#booking-form").validate();
3
4
$("#checkin").datepicker({
5
	defaultDate: "+1w",
6
	changeMonth: false,
7
	numberOfMonths: 1,
8
	prevText: '<i class="fa fa-chevron-left"></i>',
9
	nextText: '<i class="fa fa-chevron-right"></i>',
10
	onClose: function( selectedDate ) {
11
		$( "#checkout" ).datepicker( "option", "minDate", selectedDate );
12
	}
13
});
14
15
$("#checkout").datepicker({
16
	defaultDate: "+1w",
17
	changeMonth: false,
18
	numberOfMonths: 1,
19
	prevText: '<i class="fa fa-chevron-left"></i>',
20
	nextText: '<i class="fa fa-chevron-right"></i>',			
21
	onClose: function( selectedDate ) {
22
		$( "#checkin" ).datepicker( "option", "maxDate", selectedDate );
23
	}
24
});
25
</script>

You will have to replace booking-form with the id that you have set for your form, and it will automatically start validating the fields. The image below shows how the error messages show up by default after you start validation.

Form validation error exampleForm validation error exampleForm validation error example

You can pass a couple of options when initializing the date pickers. They will determine the UI of the calendar, like the number of months to show or the date from which the calendars should be shown. For example, setting defaultDate to +100w will open the calendars 100 weeks into the future.

One more thing worth noticing is that after you have selected a date, the check-in calendar sets the minimum date for the checkout calendar to be the selected date. This is done by the callback function attached to the onClose event. The checkout calendar does something similar, but it sets the maximum date for the check-in calendar.

Date picker calendar UIDate picker calendar UIDate picker calendar UI

Using a Different Theme

One of the advantages of using the Smart Forms script is that it allows you to quickly change the theme for any forms you create by simply including the relevant theme file and adding an appropriate CSS class to different elements. I will explain the process here step by step.

1. Include the CSS Theme File

The theme files are located in the directory css/smart-themes. You can pick one of those files and include it inside the booking form page. We will use red.css for this example. Your HTML code where you added the original CSS files should look like the following snippet now.

1
<link rel="stylesheet" href="css/smart-forms.css">
2
<link rel="stylesheet" href="css/smart-themes/red.css">
3
<link rel="stylesheet" href="css/font-awesome.min.css">

2. Add Appropriate Classes

Reloading the page now will not show you any visible changes in the appearance or color scheme. However, you can click on the date picker to see that the name of the month is now indeed in red. This confirms that the CSS was loaded properly.

You can change the header to red by replacing

1
<div class="form-header header-primary">

with the following HTML:

1
<div class="form-header header-red">

Similarly, you can change the color of the icon form elements and when they are active by adding the class theme-red beside form-body. Here is an example:

1
<div class="form-body theme-red">

Finally, you can change the color of remaining elements like buttons, options (checkboxes and radio buttons), and switches by using the classes btn-red, option-red, and switch-red respectively.

1
<button type="submit" class="button btn-red">Confirm Booking</button>
2
<label class="switch switch-red">

Adding CAPTCHA and Validating Form Fields

You can add more enhancements like form validation with custom error messages and basic CAPTCHA protection. All these features are built into smart form scripts. You just have to figure out how to integrate them in your form. Hopefully, this section will help you understand the whole process a little better.

The AJAX functionality and CAPTCHA are based on using PHP and JavaScript together, so you need to make sure that the form is placed on a PHP page. The value of the action attribute in the form should point to a file called smartprocess.php.

1
<form method="post" action="php/smartprocess.php" id="booking-form">

You can add a CAPTCHA to your form by adding the following HTML wherever you would like the CAPTHCA to appear.

1
<div class="section">
2
	<div class="smart-widget sm-left sml-120">
3
		<label class="field">
4
			<input type="text" name="captcha" id="captcha" class="gui-input sfcode" maxlength="6" placeholder="Enter CAPTCHA">
5
		</label>
6
		<label class="button captcode">
7
			<img src="php/captcha/captcha.php?<?php echo time();?>" id="captchax" alt="captcha">
8
			<span class="refresh-captcha"><i class="fa fa-refresh"></i></span>
9
		</label>
10
	</div>
11
</div>

The reloaded form page should now contain a captcha similar to the image below.

Smart Forms CaptchaSmart Forms CaptchaSmart Forms Captcha

You also need to update the JavaScript in your form with the following version.

1
jQuery(document).ready(function($){
2
    
3
	function reloadCaptcha(){ $("#captchax").attr("src","php/captcha/captcha.php?r=" + Math.random()); }
4
				
5
	$('.captcode').click(function(e){
6
		e.preventDefault();
7
		reloadCaptcha();
8
	});
9
	
10
	function swapButton(){
11
		var txtswap = $(".form-footer button[type='submit']");
12
		if (txtswap.text() == txtswap.data("btntext-sending")) {
13
			txtswap.text(txtswap.data("btntext-original"));
14
		} else {
15
			txtswap.data("btntext-original", txtswap.text());
16
			txtswap.text(txtswap.data("btntext-sending"));
17
		}
18
	}
19
20
	/* @custom validation method (smartCaptcha) 

21
	------------------------------------------------------------------ */	
22
	$.validator.methods.smartCaptcha = function(value, element, param) {
23
			return value == param;
24
	};
25
	
26
	$("#booking-form").validate({
27
											
28
		errorClass: "state-error",
29
		validClass: "state-success",
30
		errorElement: "em",
31
		onkeyup: false,
32
		onclick: false,						
33
		rules: {
34
				guestname: {
35
					required: true,
36
					minlength: 3
37
				},		
38
				guestemail: {
39
					required: true,
40
					email: true
41
				},
42
				guestphone: {
43
					required: true
44
				},
45
				adults: {
46
					required: true,
47
					number: true
48
				},								
49
				children: {
50
					required: true,
51
					number: true
52
				},
53
				checkin:{
54
					required:true	
55
				},
56
				checkout:{
57
					required:true	
58
				},
59
				captcha:{
60
					required:true,
61
					remote:'php/captcha/process.php'
62
				}								
63
		},
64
		messages:{
65
				guestname: {
66
						required: 'Please enter your name.',
67
						minlength: 'Name must be at least 2 characters.'
68
				},				
69
				guestemail: {
70
						required: 'Please enter your email address.',
71
						email: 'Please enter a valid email address.'
72
				},
73
				adults: {
74
						required: 'Please enter the number of adult guests.',
75
						number: 'Please enter a valid number.'
76
				},														
77
				children: {
78
						required: 'Confirm the number of child guests.',
79
						number: 'Please enter a valid number.'
80
				},	
81
				checkin:{
82
						required: 'Please select a checkin date.'
83
				},
84
				checkout:{
85
						required:'Please select a checkout date.'
86
				},
87
				captcha:{
88
						required: 'You must enter the captcha code',
89
						remote:'Captcha code is incorrect'
90
				}								
91
		},
92
		highlight: function(element, errorClass, validClass) {
93
				$(element).closest('.field').addClass(errorClass).removeClass(validClass);
94
		},
95
		unhighlight: function(element, errorClass, validClass) {
96
				$(element).closest('.field').removeClass(errorClass).addClass(validClass);
97
		},
98
		errorPlacement: function(error, element) {
99
			if (element.is(":radio") || element.is(":checkbox")) {
100
					element.closest('.option-group').after(error);
101
			} else {
102
					error.insertAfter(element.parent());
103
			}
104
		},				
105
		submitHandler:function(form) {
106
			$(form).ajaxSubmit({
107
					target:'.result',			   
108
					beforeSubmit:function(){
109
							swapButton();
110
							$('.form-footer').addClass('progress');
111
					},
112
					error:function(){
113
							swapButton();
114
							$('.form-footer').removeClass('progress');
115
					},
116
						success:function(){
117
							swapButton();
118
							$('.form-footer').removeClass('progress');
119
							$('.alert-success').show().delay(7000).fadeOut();
120
							$('.field').removeClass("state-error, state-success");
121
							if( $('.alert-error').length == 0){
122
								$('#booking-form').resetForm();
123
								reloadCaptcha();
124
							}
125
						}
126
				});
127
		}
128
		
129
	});	
130
	 
131
	$("#checkin").datepicker({
132
		defaultDate: "+1w",
133
		changeMonth: false,
134
		numberOfMonths: 1,
135
		prevText: '<i class="fa fa-chevron-left"></i>',
136
		nextText: '<i class="fa fa-chevron-right"></i>',
137
		onClose: function( selectedDate ) {
138
			$( "#checkout" ).datepicker( "option", "minDate", selectedDate );
139
		}
140
	});
141
	 
142
	$("#checkout").datepicker({
143
		defaultDate: "+1w",
144
		changeMonth: false,
145
		numberOfMonths: 1,
146
		prevText: '<i class="fa fa-chevron-left"></i>',
147
		nextText: '<i class="fa fa-chevron-right"></i>',            
148
		onClose: function( selectedDate ) {
149
			$( "#checkin" ).datepicker( "option", "maxDate", selectedDate );
150
		}
151
	});
152
});

This will take care of showing more helpful error messages right below the improperly filled form elements. The requirements that need to be fulfilled for a form element input to be considered valid are listed under rules, and the error messages that are shown to the user are placed inside the messages object.

The following image shows how the error messages would appear if users try to submit a form with invalid fields.

Smart Forms Input ValidationSmart Forms Input ValidationSmart Forms Input Validation

Final Thoughts

In this tutorial, we learned how to create a booking form with the Smart Forms script available on CodeCanyon. During the process, we covered some basics to help you get started quickly and create your own unique forms. As you can see, it is very easy to create nice-looking forms with input validation and other enhancements using Smart Forms.

You can create a lot more than just booking forms with the Smart Forms framework. The process of creating the markup and adding JavaScript functionality will still be similar to what we have covered here. If you ever feel stuck, the detailed documentation will help you out. You also get access to free support for six months, so figuring out how to make a form work would be very easy.

My advice would be that you begin by making small changes to the starter templates provided in the download file and then begin creating custom forms from scratch.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.