<!DOCTYPE html>
<html lang="en-US">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Php Form Builder - Several Ajax forms on the same page - Bootstrap 5 version</title>
    <meta name="description" content="Bootstrap 5 Ajax Form Generator - how to add several Ajax forms on the same page">
    <link rel="canonical" href="https://www.phpformbuilder.pro/templates/bootstrap-5-forms/ajax-loaded-multiple-forms.html" />

    <!-- Bootstrap 5 CSS -->

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <!-- Bootstrap icons -->

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">

    <!-- demo styles -->

    <link rel="stylesheet" href="../assets/stylesheets/code-preview-styles.min.css">
</head>

<body>

    <h1 class="text-center">Php Form Builder - Several Ajax forms on the same page<br><small>Bootstrap 5 version</small></h1>

    <div id="ajax-form-1"></div>

    <!-- Button trigger modal -->
    <div class="text-center">
        <a class="btn btn-primary text-white btn-lg" href="#" data-micromodal-trigger="modal-ajax-join-us-form-modal">Join us for FREE<i class="fas fa-unlock fa-lg fa-fw pull-right"></i></a>
    </div>

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-11 col-lg-10">
                <p class="lead">This template is loading two forms with Ajax on the same page.<br>The 1st form opens inside a modal (<em>"Join us for FREE"</em>)<br>The 2nd is the contact form below.</p>

                <hr>
                <p>The Ajax forms source code is not available on this page, you'll find it in the <em>templates/ajax-forms</em> folder
                </p>
                <div class="text-center mb-5">
                    <a href="https://www.phpformbuilder.pro/documentation/class-doc.php#ajax-loading" class="btn btn-secondary btn-sm"><strong>Ajax loading</strong> - documentation here <i class="bi bi-arrow-right ms-2"></i></a>
                </div>
                <div id="ajax-form-2"></div>
            </div>
        </div>
    </div>

    <!-- Bootstrap 5 JavaScript -->

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    <!-- Ajax form loader -->

    <script src="../../phpformbuilder/plugins/ajax-data-loader/ajax-data-loader.min.js"></script>

    <script>
        // Setup your forms below in the "ajaxForms" variable
        var ajaxForms = [
            {
                formId: 'ajax-join-us-form-modal',
                container: '#ajax-form-1',
                url: 'ajax-forms/join-us-form-modal.php'
            },
            {
                formId: 'ajax-contact-form-1',
                container: '#ajax-form-2',
                url: 'ajax-forms/contact-form-1.php'
            }
        ];

        // --- NO NEED TO CHANGE ANYTHING AFTER THIS LINE ---
        // --- COPY/PASTE THE FOLLOWING CODE IN THE HTML FILE THAT WILL LOAD THE FORM ---

        document.addEventListener('DOMContentLoaded', function() {
            ajaxForms.forEach(function(currentForm) {
                const $formContainer = document.querySelector(currentForm.container);
                if (typeof($formContainer.dataset.ajaxForm) === 'undefined') {
                    fetch(currentForm.url)
                    .then((response) => {
                        return response.text()
                    })
                    .then((data) => {
                        $formContainer.innerHTML = '';
                        $formContainer.dataset.ajaxForm = currentForm;
                        $formContainer.dataset.ajaxFormId = currentForm.formId;
                        loadData(data, currentForm.container);
                    }).catch((error) => {
                        console.log(error);
                    });
                }
            });
        });
    </script>
</body>

</html>
