The HTML Form is one of the most simple and widely used elements of the HTML language. It is normally used to create HTML forms, which are used for submitting data from a Web form to the server for processing. You can use HTML Form Tags to create a form on your web page which is used to allow your users to enter data that will be sent to the specified mail address.
To create a form, you will use above-mentioned HTML Tags, for example:
<form> <input type=”text” name=”name”> </form>
The above example will create a form that has one input field named “name”. When you enter data in the name field and hit the submit button, the form data is submitted to the specified mail address.
It’s very easy to create HTML Form with sendMail() function.
<form> <input type=”text” name=”name”> <input type=”text” name=”email”> <input type=”text” name=”subject”> <input type=”text” name=”message”> <input type=”submit” value=”send email”> </form>
We can make our form more interesting by adding a button for sending text messages and messages will be displayed in popup window.
<form> <input type=”text” name=”name”> <input type=”text” name=”email”> <input type=”text” name=”subject”> <input type=”text” name=”message”> <input type=”submit” value=”send text message”> <input type=”submit” value=”send email”> </form> <script> function sendMessage() { var msg = document.getElementById(‘subject’).value; if (msg) { alert(‘Message is : ‘+msg); } else { alert(‘Message is required’); } } </script>
This will create a form with two buttons, one for sending text messages and another for sending emails. When the button is clicked, a popup window will be displayed with the message.
This javascript code can be easily modified to send emails with attachments. In order to attach files while sending mail, we need to use the “attach()” function.
Send Email from HTML Form
<form id=”html_form” action=”” method=”post”> <p>Name: <input type=”text” name=”name”><br /> Email: <input type=”text” name=”email”><br /> Subject: <input type=”text” name=”subject”><br /> Message: <textarea id=’message’ name=’message’ rows=’4′ cols=’40’> </textarea><br /> <input type=”submit” value=”send email”> </form>
Now add following code to the above form.
<script> function sendMessage() { var msg = document.getElementById(‘subject’).value; if (msg) { alert(‘Message is : ‘+msg); } else { alert(‘Message is required’); } } </script>
This will create a form with two buttons, one for sending text message and another for sending email. When the button is clicked, a popup window will be displayed with the message.
This javascript code can be easily modified to send emails with attachments. In order to attach files while sending mail, we need to use the “attach()” function.
Source – headlessforms.cloud