skip to Main Content

Below I have my HTML code that includes two forms, they are both the same except for the name field that only does what it is intended to do when a form is before it (doesn’t matter if it’s displayable or not).

    <div class="contact-right">
        <form class="contact-form" style="display: none;">
            <input id="form-name" name="Name" type="text" placeholder="Your Name" required>
            <input id="form-email" name="Email" type="email" placeholder="Your Email" required>
            <textarea id="form-message" name="Message" type="text" placeholder="Your Message"></textarea>
            <button id="send-button" type="submit">Send</button>
        </form>
        <form name="submit-to-google-sheet" class="contact-form">
            <input id="form-name" name="Name" type="text" placeholder="Your Name" required>
            <input id="form-email" name="Email" type="email" placeholder="Your Email" required>
            <textarea id="form-message" name="Message" type="text" placeholder="Your Message"></textarea>
            <button id="send-button" type="submit">Send</button>
        </form>
    </div>
</section>
<div class="footer">
    <p>Stuff Here</p>
</div>

<script>
    const scriptURL = '<SCRIPT URL>'
    const form = document.forms['submit-to-google-sheet']
  
    form.addEventListener('submit', e => {
      e.preventDefault()
      fetch(scriptURL, { method: 'POST', body: new FormData(form)})
        .then(response => console.log('Success!', response))
        .catch(error => console.error('Error!', error.message))
    })
  </script>

** I used to protect the URL I was using

The intended use is to have a single form that gets the text elements of the form inputs and stores it on a Google Sheet (followed: https://github.com/jamiewilson/form-to-google-sheets). I’ve tried so many angles and it always seems to be that the form with the submit-to-google-sheet doesn’t run if it’s the only form.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out my javascript which was purposefully getting HTML elements by id would inhibit the actions that the form component was trying to do (I put in my own email checkers).

    I ended up deleting the prior form and javascript portion of my code that references those components, I'm pretty sure because it detected the prior form the second one was unaffected.


  2. I don’t think that this works because of the presence of another form. Please ensure the following:

    1. delete the previous triggers and create a new trigger. In the github link you’ve given, there are two functions given. So ensure that the function you’re selecting must be doPost and not the other one.

    2. Trigger must be on ‘onSubmit’. By mistake you should not select ‘onEdit’

    3. Before deploying run the initial setup once.

    4. Ensure the name of the sheet. It is given as ‘Sheet1’. Don’t rename it though I couldn’t see any relation between the sheet_name and the function

    5. The headers of the sheet must exactly match the ‘name’ mentioned in each input tag.

    6. Save it before publishing.

    7. Publish as webapp.

    8. Publish as a new version.

    9. Access to anybody including anonymous

    10. copy the new url given by oath and paste in the place of <SCRIPT_URL>. Ensure quotes both sides. (Both should be either single or either double. No mismatching)

    11. Try placing the javascript inside the head tag. Ensure that it is inside script tags. Or place it along with other javascript in a separate js file. provide link to it.

    12. Try this also. Don’t think that jquery and document-ready is useless. Sometimes, I’ve seen incidents that it does wonders with google appscript. This could be my perception even. But try. Provide a jquery Cdn link and place the code inside $( document ).ready(function() { })

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search