Would you like to know how to help users get in touch with you on your website without displaying your contact info where bots can find it and spam your email? A simple solution is to add a contact form to your site to allow users to send you a message without sharing your email with everyone on the Internet.
Follow our tips below to learn how to create a basic form in HTML and see how you can make use of PHP or JavaScript to make it functional.
Is a Form Necessary?
Yes. A form is a great way to get to know the users visiting your site.
With a form, you are able to build a database to be able to send newsletters about campaigns or new content, advertise discounts, new products, new services, etc. the possibilities almost seem endless with forms.
Form Elements
The basic element to create a form in HTML is the < form > tag. This tag includes other elements such as < input >, < textarea >, < button >, < label >, etc.
HTML Structure:
< form action=”/example.php” method=”get” >
< label for=”fname” >First Name:</ label >
< input type=”text” id=”fname” name=”fname” >
< input type=”submit” value=”Submit” >
</ form >

The above code will submit the user’s First Name (fname) and send it to the php file on the server called example.php.
The < input > tag specifies the input field where users are able to add their information.
HTML Structure:
< form action=”/example.php” method=”get” >
< label for=”fname”>First Name:</ label >
< input type=”text” id=”fname” name=”fname” >
< input type=”submit” value=”Submit” >
</ form >

Input Types
There are various types of input for input elements that allows users to input data other than simple text:
- button
- checkbox
- color
- date
- datetime-local
- file
- hidden
- image
- month
- number
- week
- password
- radio
- range
- reset
- search
- submit
- tel
- text – This is the default value
- time
- url
The < select > tag is a useful tag if you want to add a drop-down list to your form, allowing users to choose from a range of options.
HTML Structure:
< label for=”services” >Choose a Service:</ label >
< select name=”service” id=”service” >
< option value=”it-services” >IT Services & Support</ option >
< option value=”aws” >Amazon Web Services</ option >
< option value=”awsdevops” >Amazon Web Services DevOps</ option >
< option value=”microsoft” >Microsoft 365</ option >
< option value=”seo” >SEO & Digital Marketing</ option >
</ select >
< input type=”submit” value=”Submit” >

Multiple Selections
By using the “multiple” attribute, users are able to select more than one option.
HTML Structure:
< label for=”services” >Choose a Service:</ label >
< select name=”service” id=”service” multiple >
< option value=”it-services” >IT Services & Support</ option >
< option value=”aws” >Amazon Web Services</ option >
< option value=”awsdevops” >Amazon Web Services DevOps</ option >
< option value=”microsoft” >Microsoft 365</ option >
< option value=”seo” >SEO & Digital Marketing</ option >
</ select >
Other select attributes include autofocus, disabled, form, multiple, name, required, and size.
The < textarea > tag defines a multiline area for users to input information, often used to add a message with other form details.
HTML Structure:
< label for=”message” >Message:</ label >
< textarea id=”message” name=”message” rows=”4″ cols=”50″ >
</ textarea >
< input type=”submit” value=”Submit” >

The
HTML Structure:
< form action=”/example.php” method=”get” >
< label for=”fname”>First Name:</ label >
< input type=”text” id=”fname” name=”fname” >
< input type=”submit” value=”Submit” >
</ form >

The < fieldset > groups form elements together and the
tag provides a caption for < fieldset > elements.
HTML Structure:
< form action=”example.php”>
< fieldset >
< legend >User: </ legend >
< label for=”name” >Name:</ label >
< input type=”text” id=”name” name=”name “>
< label for=”surname” >Surname:</ label >
< input type=”text” id=”lname” name=”lname” >
< input type=”submit” value=”Submit” >
</ fieldset >
</ form >

The < button > tag creates a clickable button.
HTML Structure:
< button type=”button”>Click Here</ button >

Other attributes that can be used with the button element includes autofocus, disabled, form, formaction, formenctype, formmethod, formnovalidate, formtarget, name, type, and value.
The < optgroup > tag groups different < option > tags in one group. These option tags are used within a < select > element.
HTML Structure:
< label for=”services” >Choose a Service:</ label >
< select name=”service” id=”service”>
< optgroup label=”IT Services”>
< option value=”it-services” >IT Services & Support</ option >
</ optgroup >
< optgroup label=”Cloud Services” >
< option value=”aws” >Amazon Web Services</ option >
< option value=”awsdevops” >Amazon Web Services DevOps</ option >
< option value=”azure” >Microsoft Azure</ option >
</ optgroup >
</ select >
< input type=”submit” value=”Submit” >

The < datalist > tag provides pre-defined options while allowing the user to type their input. Think about Google’s search bar, as you type in keywords, suggestions start displaying at the bottom.
HTML Structure:
< form action=”example.php” method=”get” >
< label for=”services” >Choose a service from the list:</ label >
< input list=”services” name=”services” id=”services” >
< datalist id=”services”>
< option value=”IT Services” >
< option value=”Cloud Services” >
< option value=”Digital Marketing Services” >
< option value=”Microsoft Services” >
</ datalist >
< input type=”submit” >

The < output > tag performs a calculation and displays the result in the < output > element.
< form oninput=”x.value=parseInt(a.value)+parseInt(b.value)”>
< input type=”range” id=”a” value=”50″>
+< input type=”number” id=”b” value=”25″>
=< output name=”x” for=”a b”></ output >
</ form >

Build a Basic Form
Now you are ready to build a basic form in HTML.
The form below is only showcasing the basic code structure needed to build the form, you still need to make use of PHP or JavaScript to make the form functional.
< form action=”/example.php” >
< label for=”fname”>First name:</ label >
< input type=”text” id=”fname” name=”fname” >
< label for=”lname”>Last name:
< input type=”text” id=”lname” name=”lname” >
< label for=”message”>Message:
< textarea id=”message” name=”message” rows=”4″ cols=”50″>
< input type=”submit” value=”Submit”>
</ form>
Adding Functionality with PHP or JavaScript
To ensure your form works when a user enters their information and clicks on the submit button, you need to add PHP or JavaScript elements.
How to Add Form Functionality with PHP
In PHP, forms make use of $_GET and $_Post to collect form data.
When to use:
- Information sent via the $_GET method is visible to everyone.
- $_POST is used when the form contains sensitive information.
Follow this link to see how to create a basic form with HTML and PHP.
How to Add Form Functionality with JavaScript
Step 1: Create your HTML form
Step 2: Create your Javascript file and include it in your HTML file
Step 3: Add some CSS styling (this is optional, although a good looking form will be better)
Follow the steps below to create a simple form with HTML and Javascript:
https://www.formget.com/javascript-contact-form/
Required
If you need specific information from users, you can specify that that field is mandatory or required. This attribute will ensure a message displays that the user cannot submit the form without entering information in the required fields.
HTML Structure:
< form action=”/example.php”>
< label for=”fname” >First name:
< input type=”text” id=”fname” name=”fname” required >
< input type=”submit” value=”Submit” >
</ form>
Now that you are able to create forms in HTML, it’s time we move on to some CSS! This is where we start making your HTML code look amazing by styling all the elements.
Bookmark our Blog or like and follow our Facebook and LinkedIn page to continue learning with us.