In the realm of web development, Bootstrap has long been a trusted ally, offering a powerful and responsive CSS framework. One of its standout features is the modal, a dialog box that appears on top of the current page, providing an interactive way to display content. In Bootstrap 5, modals have been enhanced with new features and improved accessibility. Today, we're going to delve into the world of Bootstrap 5 modals, with a special focus on the submit functionality.

Before we dive into the submit aspect, let's ensure we have a solid understanding of Bootstrap 5 modals. They're essentially a layer of content that appears on top of the current page, with a backdrop that prevents interaction with the rest of the page. This makes them perfect for displaying important information, forms, or even full-screen images and videos.

Setting Up a Bootstrap 5 Modal
To get started with a Bootstrap 5 modal, you'll first need to include the Bootstrap CSS and JavaScript files in your project. Once that's done, you can create a modal using the following HTML structure:

<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
... Your content here ...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Modal Triggers and Targets

Modals can be triggered by various events, such as button clicks or keyboard inputs. The trigger element should have a data-bs-target attribute that points to the ID of the modal. For example, if your modal has an ID of "myModal", your trigger button would look like this:
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Launch demo modal</button>
Modal States and Sizes

Bootstrap 5 modals can be displayed in different states (like small, medium, large, and extra large) and sizes (fullscreen for mobile devices). You can achieve this by adding the appropriate CSS class to the modal-dialog element. For instance, to create a large modal, you would use the class "modal-dialog modal-lg".
Submitting Forms in Bootstrap 5 Modals
Modals are often used to display forms, and Bootstrap 5 makes it easy to submit these forms using the built-in modal functionality. To do this, you'll need to use the data-bs-dismiss attribute with the value "modal" on your submit button. This tells Bootstrap to close the modal after the form is submitted.

Preventing Form Submission on Page Refresh
By default, when a form is submitted, the browser will refresh the page. To prevent this and ensure that the modal closes as expected, you'll need to add the following JavaScript code:




















document.querySelectorAll('.modal form').forEach(form => {
form.addEventListener('submit', event => {
event.preventDefault();
form.reset();
});
});
Submitting Form Data
To actually submit the form data, you can use the Fetch API or jQuery's AJAX functionality. Here's an example using Fetch:
document.querySelectorAll('.modal form').forEach(form => {
form.addEventListener('submit', async event => {
event.preventDefault();
const formData = new FormData(form);
const response = await fetch('/your-endpoint', {
method: 'POST',
body: formData
});
if (response.ok) {
form.reset();
form.closest('.modal').querySelector('.btn-close').click();
}
});
});
And there you have it! You now know how to create and submit forms in Bootstrap 5 modals. Whether you're displaying important information or collecting user data, modals are a versatile tool in your web development kit. So go forth and build something amazing!