JavaScript Submit form example
Forms are an integral part of any website. Most websites often contain the Contact Us form. Forms collect the user information & send it to a server to store it in a database. In today’s post, we are going to learn how you can collect the user information via HTML forms & submit it to the sever.
<form name="subscriberForm" onsubmit="return submitForm()" method="post">
<label>Name: </label>
<input required type="text" name="firstName">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Form: Forms contain a collection of HTML elements to collect user info like input, text area, select, checkbox, radio, etc. If a required attribute is specified inside the HTML elements form will make sure that the user fills in the information & only after filling in the required info it will submit the form.
To submit the form, add a button whose type is submit & specify which function should be called once the form is submitted.
$('#subscriberForm').on('submit', function (e) {
e.preventDefault();
// process the data
});
You can also submit the form via jQuery, just listen to submit event on the form & process the data.