Home > front end >  How to send HTML form data to a google sheet?
How to send HTML form data to a google sheet?

Time:09-17

I am trying to send the HTML form data to google sheet. I have followed a couple of tutorials on that but am unable to send form data to the google sheet. I successfully deployed the google script copied the link and linked to the HTML but no result. What is actually I am missing here? Seeking your guidance.

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Form</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

</head>

<body>
    <nav class="navbar navbar-light bg-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">
                <img src="https://www.freeiconspng.com/uploads/tumblr-logo-icon-9.png" alt="" width="30" height="24" class="d-inline-block align-text-top"> Test Form
            </a>
        </div>
    </nav>

    <div class="container">

        <form class="row g-3" method="POST" autocomplete="off" name="test_form">

            <select class="form-select form-select-lg mb-3" name="title" aria-label=".form-select-lg example">
                <option selected>Open this select menu</option>
                <option value="1">Mr.</option>
                <option value="2">Miss</option>
                <option value="3">Mrs.</option>
            </select>

            <div class="container">
                <div class="row g-2">
                    <div class="col-6">
                        <div class="col">
                            <input type="text" class="form-control" name="firstname" placeholder="First name" aria-label="First name">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="col">
                            <input type="text" class="form-control" name="lastname" placeholder="Last name" aria-label="Last name">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="col">
                            <input type="email" class="form-control" name="email" placeholder="Email" aria-label="Email">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="col">
                            <input type="text" class="form-control" name="telephone" placeholder="Telephone" aria-label="Telephone">
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-floating">
                <textarea class="form-control" name="comment" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px"></textarea>
                <label for="floatingTextarea2">Comments</label>
            </div>

            <div class="col-12">
                <button class="btn btn-primary" type="submit" name="submit" value="send message">Submit form</button>
            </div>
        </form>

        <script type="text/javascript">
            const scriptURL = 'https://script.google.com/macros/s/AKfycbyWrl6cQr8C6PatE2tTZ3kJWi0lInw4rgu4nRBB8AqY3X7GDJ_6387yOtqsBJSjdV96og/exec'
            const form = document.forms['google-sheet']

            form.addEventListener('submit', e => {
                e.preventDefault()
                fetch(scriptURL, {
                        method: 'POST',
                        body: new FormData(form)
                    })
                    .then(response => alert("Thanks for Contacting us..! We Will Contact You Soon..."))
                    .catch(error => console.error('Error!', error.message))
            })
        </script>
        <!-- <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> -->
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u LWgxfKTRIcfu0JTxR EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
 
    </div>
</body>

</html>

CodePudding user response:

You can follow tutorial from here.

Make sure to add Add a new project trigger, Publish the project as a web app and Input your web app URL carefully.

  • Related