<!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 - Material Design Bootstrap 4</title>
    <meta name="description" content="Material Ajax Form Generator - how to add several Ajax forms on the same page">
    <link rel="canonical" href="https://www.phpformbuilder.pro/templates/material-bootstrap-forms/ajax-loaded-multiple-forms.html" />

    <!-- Bootstrap 4 CSS -->

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <!-- Font awesome icons -->

    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/solid.css" integrity="sha384-Tv5i09RULyHKMwX0E8wJUqSOaXlyu3SQxORObAI08iUwIalMmN5L6AvlPX2LMoSE" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/fontawesome.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">

    <!-- demo styles -->

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

    <!-- We have to load the Materialize plugin here instead of adding it to the Ajax forms to avoid Javascript errors. -->
    <link href="../../phpformbuilder/plugins/materialize/dist/css/material-forms.min.css" rel="stylesheet" media="screen">
    <script type="text/javascript" src="../../phpformbuilder/plugins/materialize/dist/js/material-forms.min.js"></script>
</head>

<body>

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

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

    <!-- Button trigger modal -->
    <div class="text-center">
        <button type="button" class="btn btn-primary waves-effect waves-light text-white btn-lg" data-micromodal-trigger="modal-ajax-join-us-form-modal">Join us for FREE<i class="fas fa-unlock ml-2"></i></button>
    </div>

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-11 col-lg-10">
                <p>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="fas fa-arrow-right ml-2"></i></a>
                </div>
                <div id="ajax-form-2"></div>
            </div>
        </div>
    </div>

    <!-- jQuery -->

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

    <!-- Bootstrap 4 JavaScript -->

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 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>
