Submit Button
May 20, 2023
A submit button is an HTML form element that is used to initiate the submission of form data to a server. When a user clicks on a submit button, the form data is collected and sent to the server for processing. Submit buttons are commonly used in web applications for tasks such as submitting a login form, creating a new user account, or submitting a search query.
How Submit Buttons Work
Submit buttons are typically created using the HTML <button>
or <input>
elements. The <button>
element can be used to create a custom-styled button, while the <input>
element can be used to create a standard button that inherits the styling of the browser. The type
attribute of the <input>
element is set to “submit” to indicate that it is a submit button.
When a user clicks on a submit button, the browser collects the form data and sends it to the server using the HTTP POST method. The data is typically sent in the form of name-value pairs, where the name corresponds to the name of the form field and the value corresponds to the value entered by the user.
It is important to note that a submit button alone does not process the form data. The server-side code must be written separately to handle the form data and perform any necessary actions, such as storing the data in a database or sending an email.
Styling Submit Buttons
Submit buttons can be styled using CSS to match the design of the website. The <button>
element can be styled using the background-color
, color
, and border
properties, among others. The <input>
element can be styled using the background-color
, color
, border
, and outline
properties.
For example, the following CSS code can be used to style a submit button with a blue background and white text:
button[type=submit], input[type=submit] {
background-color: blue;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
}
Handling Form Submission with JavaScript
In addition to submitting form data to the server, submit buttons can also be used to trigger JavaScript functions that perform client-side validation or other tasks. This can be done using the onclick
attribute of the <button>
element or the addEventListener()
method in JavaScript.
For example, the following HTML and JavaScript code can be used to validate a form before submitting it to the server:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<button type="submit" onclick="validateForm()">Submit</button>
</form>
<script>
function validateForm() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username == "" || password == "") {
alert("Username and password are required.");
return false;
}
}
</script>
In this example, the onclick
attribute of the submit button calls the validateForm()
function, which checks that the username and password fields are not empty. If either field is empty, an alert is displayed and the form submission is cancelled.
Accessibility Considerations
When using submit buttons in web applications, it is important to consider accessibility for users with disabilities. Submit buttons should be clearly labeled using the value
attribute of the <button>
or <input>
element, and should provide visual feedback when clicked using CSS.
In addition, it is important to ensure that submit buttons can be activated using the keyboard alone, without requiring the use of a mouse or other pointing device. This can be achieved by using the tabindex
attribute to specify the order in which form elements are navigated using the keyboard.