Javascript Submitted Contact Form

Submitting a simple contact form using vanilla Javascript. Styled using Bootstrap.

Stay in the know

Get all the latest developments and special offers from StaticForms.

<!-- Subscribe Section -->
<div class="position-relative space-bottom-2">
    <div class="container space-top-2 space-top-md-3" id="form-container">
        <!-- Title -->
        <div class="text-center w-md-60 mx-auto mb-7">
            <h2 class="text-primary"><span class="font-weight-semi-bold">Stay</span> in the know</h2>
            <p>Get all the latest developments and special offers from StaticForms.</p>
        </div>
        <!-- End Title -->

        <!-- Subscribe Form -->
        <div class="w-md-50 w-lg-40 mx-auto">
            <form id="sign-up-form" type="submit" action="POST">
                <label class="sr-only" for="subscribeSrEmailExample1">Enter your email address</label>
                <div
                    class="input-group input-group-lg input-group-borderless input-group-pill shadow-lg">
                    <input type="email" class="form-control" name="email" id="email"
                        placeholder="Enter your email address" aria-label="Enter your email address"
                        aria-describedby="subscribeButtonExample1" required>
                    <div class="input-group-append">
                        <button class="btn btn-white text-primary" type="submit"
                            id="subscribeButtonExample1">
                            <span class="fas fa-paper-plane"></span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <!-- End Subscribe Form -->
    </div>
</div><!-- End Subscribe Section -->
    <script>
    const form = document.getElementById('sign-up-form');
    const formContainer = document.getElementById('form-container');
    var message = document.createElement("div");
    message.classList.add("space-3");
    form.addEventListener('submit', function (e) {
        e.preventDefault();

        fetch('https://api.staticforms.co/Submit?form=rpT2', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
            body: JSON.stringify({ "email": document.getElementById("email").value })
        })
            .then(response => {
                console.log(response);
                if (response.status == 200) {
                    message.innerHTML = "<div class='space-1 w-md-80 w-lg-60 mx-auto p-1 shadow text-center'><h2 class='text-dark'><span class='text-success font-weight-semi-bold'>Thank you</span> for signing up!</h2><p>Keep an eye on your inbox for our next newsletter.</p></div>";
                    formContainer.parentNode.replaceChild(message, formContainer);
                } else {
                    console.log(error);
                    message.innerHTML = "<div class='space-1 w-md-80 w-lg-60 mx-auto p-1 shadow text-center'><h2 class='text-dark'><span class='text-danger font-weight-semi-bold'>Oops.</span> Something went wrong!</h2><p>We're aware of the problem and investigating.</p><p>Please try again later.</p></div>";
                    formContainer.parentNode.replaceChild(message, formContainer);
                }
            })
            .catch(error => {
                console.log(error);
                message.innerHTML = "<div class='space-1 w-md-80 w-lg-60 mx-auto p-1 shadow text-center'><h2 class='text-dark'><span class='text-danger font-weight-semi-bold'>Oops.</span> Something went wrong!</h2><p>We're aware of the problem and investigating.</p><p>Please try again later.</p></div>";
                formContainer.parentNode.replaceChild(message, formContainer);
            })
    });
</script>